刚坐在车里就开始电闪雷鸣。听着隆隆的雷声,没有恐慌,到有几分振奋人心的舒心感、酣畅淋漓感。朝外望去,突然间,狂风夹杂着骤雨席卷而来,一泻千里。瞬间暴雨如注,打在地上,溅起朵朵浪花,在热浪的蒸腾下激起层层水雾。满大街是慌乱的人群,行人跑着、叫喊着,唧唧喳喳的赶着在房檐下躲雨。这时候伞是没有用的,挡不住狂风暴雨的袭击,无论是带了伞的,还是没带伞的,全都躲在了房檐下。
本文实例讲述了ASP.NET中Dictionary基本用法。分享给大家供大家参考,具体如下:
//Dictionary位于System.Collections.Generic命名空间之下 /* * 使用Dictionary之前必须引用System.Collections.Generic命名空间; * 使用Dictionary时必须声明其键和值的数据类型(可以为任意类型); */ //声明实例化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(键)和对应的quber300(值) dic.Add(300, "quber300"); } //移除dic键为300的项 dic.Remove(300); //获取dic键值对总数 int dicCount = dic.Count; Response.Write("循环获取dic中的键和值:<br/>"); //循环获取dic中的键和值 foreach (KeyValuePair<int, string> keyDic in dic) { Response.Write("key:" + keyDic.Key + ",value:" + keyDic.Value + "<br/>"); } Response.Write("<hr/><br/>"); Response.Write("循环获取dic中的键:<br/>"); //循环获取dic中的键 Dictionary<int, string>.KeyCollection keyDics = dic.Keys; foreach (int iKey in keyDics) { Response.Write("key:" + iKey + "<br/>"); } Response.Write("<hr/><br/>"); Response.Write("另一种方法循环获取dic中的键:<br/>"); //循环获取dic中的键 foreach (int iKey in dic.Keys) { Response.Write("key:" + iKey + "<br/>"); } Response.Write("<hr/><br/>"); Response.Write("循环获取dic中的值:<br/>"); //循环获取dic中的值 Dictionary<int, string>.ValueCollection valueDics = dic.Values; foreach (string strValue in valueDics) { Response.Write("value:" + strValue + "<br/>"); } Response.Write("<hr/><br/>"); Response.Write("另一种方法循环获取dic中的值:<br/>"); //循环获取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/>");
希望本文所述对大家asp.net程序设计有所帮助。
到此这篇关于ASP.NET中Dictionary基本用法实例分析就介绍到这了。在心甘情愿的浪费的过程里,青春渐渐逝去,直到消失,而我们却继续着在青春上所遗留的习惯却不自知。不知道我们在重复着浪费的举动,也不知道自己其实要给青春交出答卷,更不知道要用何种方式去祭奠青春。更多相关ASP.NET中Dictionary基本用法实例分析内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!