Analysis of Basic Usage Examples of Dictionary in ASP. NET
- 2021-08-05 09:39:30
- OfStack
This paper illustrates the basic usage of Dictionary in ASP. NET. Share it for your reference, as follows:
//Dictionary Located at System.Collections.Generic Namespace
/*
* Use Dictionary You must reference before System.Collections.Generic Namespace;
* Use Dictionary You must declare the data type of its key and value (which can be of any type);
*/
// Declare instantiation Dictionary For dic
System.Collections.Generic.Dictionary<int, string> dic = new System.Collections.Generic.Dictionary<int, string>();
// For dic Add keys and values
dic.Add(100, "quber100");
dic.Add(200, "quber200");
// Check if it exists 300 This key
if (!dic.ContainsKey(300))
{
// New additions 300 (Key) and the corresponding quber300 (Value)
dic.Add(300, "quber300");
}
// Remove dic Key is 300 Items of
dic.Remove(300);
// Get dic Total number of key-value pairs
int dicCount = dic.Count;
Response.Write(" Loop acquisition dic Keys and values in: <br/>");
// Loop acquisition dic Keys and values in
foreach (KeyValuePair<int, string> keyDic in dic)
{
Response.Write("key:" + keyDic.Key + ",value:" + keyDic.Value + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write(" Loop acquisition dic Key in: <br/>");
// Loop acquisition dic Key in
Dictionary<int, string>.KeyCollection keyDics = dic.Keys;
foreach (int iKey in keyDics)
{
Response.Write("key:" + iKey + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write(" Another 1 Loop to get in a variety of ways dic Key in: <br/>");
// Loop acquisition dic Key in
foreach (int iKey in dic.Keys)
{
Response.Write("key:" + iKey + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write(" Loop acquisition dic Values in: <br/>");
// Loop acquisition dic Value in
Dictionary<int, string>.ValueCollection valueDics = dic.Values;
foreach (string strValue in valueDics)
{
Response.Write("value:" + strValue + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write(" Another 1 Loop to get in a variety of ways dic Values in: <br/>");
// Loop acquisition dic Value in
foreach (string strValue in dic.Values)
{
Response.Write("value:" + strValue + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write(" Get dic Individual keys and values in: <br/>");
Response.Write("key:100,value:" + dic[100] + "<br/>");
Response.Write("<hr/><br/>");
Response.Write(" Check dic Whether a key exists in the ( 100 ) and returns its value dicStr : <br/>");
// Check dic Whether a key exists in the ( 100 ) and returns its value dicStr
string dicStr = string.Empty;
if (dic.TryGetValue(100, out dicStr))
{
Response.Write("OK");
}
else
{
Response.Write("NO");
}
Response.Write("<hr/><br/>");
For more readers interested in asp. net, please check the topics of this site: "Summary of asp. net Operation json Skills", "Summary of asp. net String Operation Skills", "Summary of asp. net Operation XML Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".
I hope this paper is helpful to everyone's asp. net programming.