ASP.NET中Dictionary基本用法實(shí)例分析
本文實(shí)例講述了ASP.NET中Dictionary基本用法。分享給大家供大家參考,具體如下:
//Dictionary位于System.Collections.Generic命名空間之下 /* * 使用Dictionary之前必須引用System.Collections.Generic命名空間; * 使用Dictionary時(shí)必須聲明其鍵和值的數(shù)據(jù)類型(可以為任意類型); */ //聲明實(shí)例化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這個(gè)鍵 if (!dic.ContainsKey(300)) { //新增加300(鍵)和對(duì)應(yīng)的quber300(值) dic.Add(300, "quber300"); } //移除dic鍵為300的項(xiàng) dic.Remove(300); //獲取dic鍵值對(duì)總數(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中單個(gè)鍵和值:<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/>");
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
- ASP.NET Dictionary 的基本用法示例介紹
- C#實(shí)現(xiàn)自定義Dictionary類實(shí)例
- C#針對(duì)xml文件轉(zhuǎn)化Dictionary的方法
- C#泛型集合Dictionary<K,V>的使用方法
- C#中Dictionary的作用及用法講解
- C#泛型Dictionary的用法實(shí)例詳解
- C#探秘系列(一)——ToDictionary,ToLookup
- C#中查找Dictionary中重復(fù)值的方法
- C# Hashtable/Dictionary寫入和讀取對(duì)比詳解
- C#中Dictionary幾種遍歷的實(shí)現(xiàn)代碼
相關(guān)文章
.NET的基元類型包括什么及Unmanaged和Blittable類型詳解
這篇文章主要介紹了.NET的基元類型包括什么及Unmanaged和Blittable類型詳解,Unmanaged類型可以理解不涉及托管對(duì)象引用的值類型,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06基于.NET程序默認(rèn)啟動(dòng)線程數(shù)講解
本篇文章小編為大家介紹,基于.NET程序默認(rèn)啟動(dòng)線程數(shù)講解。需要的朋友參考下2013-04-04asp.net core標(biāo)簽助手的高級(jí)用法TagHelper+Form
這篇文章主要為大家詳細(xì)介紹了asp.net core標(biāo)簽助手的高級(jí)用法TagHelper+Form,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Asp.Net FckEditor在web.config中配置的具體實(shí)例
Asp.Net FckEditor在web.config中配置的具體實(shí)例,需要的朋友可以參考一下2013-06-06asp.net URL重寫簡(jiǎn)化版 速學(xué)URL重寫
asp.net URL重寫(URLRewriter)簡(jiǎn)化版 。速學(xué)URL重寫2010-01-01asp.net下實(shí)現(xiàn)輸入數(shù)字的冒泡排序
.net下實(shí)現(xiàn)輸入數(shù)字的冒泡排序2010-03-03