溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Dictionary如何在ASP.NET中使用

發(fā)布時間:2021-04-02 16:57:56 來源:億速云 閱讀:155 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Dictionary如何在ASP.NET中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

//Dictionary位于System.Collections.Generic命名空間之下
/*
 * 使用Dictionary之前必須引用System.Collections.Generic命名空間;
 * 使用Dictionary時必須聲明其鍵和值的數(shù)據(jù)類型(可以為任意類型);
 */
//聲明實例化Dictionary為dic
System.Collections.Generic.Dictionary<int, string> dic = new System.Collections.Generic.Dictionary<int, string>();
//為dic添加鍵和值
dic.Add(100, "quber100");
dic.Add(200, "quber200");
//檢查是否存在300這個鍵
if (!dic.ContainsKey(300))
{
  //新增加300(鍵)和對應(yīng)的quber300(值)
  dic.Add(300, "quber300");
}
//移除dic鍵為300的項
dic.Remove(300);
//獲取dic鍵值對總數(shù)
int dicCount = dic.Count;
Response.Write("循環(huán)獲取dic中的鍵和值:<br/>");
//循環(huán)獲取dic中的鍵和值
foreach (KeyValuePair<int, string> keyDic in dic)
{
  Response.Write("key:" + keyDic.Key + ",value:" + keyDic.Value + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write("循環(huán)獲取dic中的鍵:<br/>");
//循環(huán)獲取dic中的鍵
Dictionary<int, string>.KeyCollection keyDics = dic.Keys;
foreach (int iKey in keyDics)
{
  Response.Write("key:" + iKey + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write("另一種方法循環(huán)獲取dic中的鍵:<br/>");
//循環(huán)獲取dic中的鍵
foreach (int iKey in dic.Keys)
{
  Response.Write("key:" + iKey + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write("循環(huán)獲取dic中的值:<br/>");
//循環(huán)獲取dic中的值
Dictionary<int, string>.ValueCollection valueDics = dic.Values;
foreach (string strValue in valueDics)
{
  Response.Write("value:" + strValue + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write("另一種方法循環(huán)獲取dic中的值:<br/>");
//循環(huán)獲取dic中的值
foreach (string strValue in dic.Values)
{
  Response.Write("value:" + strValue + "<br/>");
}
Response.Write("<hr/><br/>");
Response.Write("獲取dic中單個鍵和值:<br/>");
Response.Write("key:100,value:" + dic[100] + "<br/>");
Response.Write("<hr/><br/>");
Response.Write("檢查dic中是否存在鍵(100),并返回其值dicStr:<br/>");
//檢查dic中是否存在鍵(100),并返回其值dicStr
string dicStr = string.Empty;
if (dic.TryGetValue(100, out dicStr))
{
  Response.Write("OK");
}
else
{
  Response.Write("NO");
}
Response.Write("<hr/><br/>");

看完上述內(nèi)容,你們對Dictionary如何在ASP.NET中使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI