• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ASP.NET中Dictionary基本用法实例分析

asp 搞代码 4年前 (2022-01-03) 33次浏览 已收录 0个评论

这篇文章主要介绍了ASP.NET中Dictionary基本用法,结合实例形式分析了Dictionary的基本功能、使用步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了ASP.NET中Dictionary基本用法。分享给大家供大家参考,具体如下:

 //Dictionary位于System.Collections.Generic命名空间之下 /* * 使用Dictionary之前必须引用System.Collections.Generic命名空间; * 使用Dictionary时必须声明其键和值的数据类型(可以为任意类型); */ //声明实例化Dictionary为dic System.Collections.Generic.Dictionary dic = new System.Collections.Generic.Dictionary(); //为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 keyDic in dic) { Response.Write("key:" + keyDic.Key + ",value:" + keyDic.Value + "<br />"); } Response.Write("<hr /><br />"); Response.Write("循环获取dic中的键:<br />"); //循环获取dic中的键 Dictionary.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.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操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

以上就是ASP.NET中Dictionary基本用法实例分析的详细内容,更多请来源gaodai#ma#com搞@代~码网关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ASP.NET中Dictionary基本用法实例分析
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址