您现在的位置是:网站首页> 编程资料编程资料
ASP.NET中Dictionary基本用法实例分析_实用技巧_
2023-05-24
449人已围观
简介 ASP.NET中Dictionary基本用法实例分析_实用技巧_
本文实例讲述了ASP.NET中Dictionary基本用法。分享给大家供大家参考,具体如下:
//Dictionary位于System.Collections.Generic命名空间之下 /* * 使用Dictionary之前必须引用System.Collections.Generic命名空间; * 使用Dictionary时必须声明其键和值的数据类型(可以为任意类型); */ //声明实例化Dictionary为dic System.Collections.Generic.Dictionarydic = 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中的键和值:
"); //循环获取dic中的键和值 foreach (KeyValuePairkeyDic in dic) { Response.Write("key:" + keyDic.Key + ",value:" + keyDic.Value + "
"); } Response.Write("
"); Response.Write("循环获取dic中的键:
"); //循环获取dic中的键 Dictionary.KeyCollection keyDics = dic.Keys; foreach (int iKey in keyDics) { Response.Write("key:" + iKey + "
"); } Response.Write("
"); Response.Write("另一种方法循环获取dic中的键:
"); //循环获取dic中的键 foreach (int iKey in dic.Keys) { Response.Write("key:" + iKey + "
"); } Response.Write("
"); Response.Write("循环获取dic中的值:
"); //循环获取dic中的值 Dictionary.ValueCollection valueDics = dic.Values; foreach (string strValue in valueDics) { Response.Write("value:" + strValue + "
"); } Response.Write("
"); Response.Write("另一种方法循环获取dic中的值:
"); //循环获取dic中的值 foreach (string strValue in dic.Values) { Response.Write("value:" + strValue + "
"); } Response.Write("
"); Response.Write("获取dic中单个键和值:
"); Response.Write("key:100,value:" + dic[100] + "
"); Response.Write("
"); Response.Write("检查dic中是否存在键(100),并返回其值dicStr:
"); //检查dic中是否存在键(100),并返回其值dicStr string dicStr = string.Empty; if (dic.TryGetValue(100, out dicStr)) { Response.Write("OK"); } else { Response.Write("NO"); } Response.Write("
");
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。
您可能感兴趣的文章:
相关内容
- ASP.NET简单获取服务端和客户端计算机名称的方法_实用技巧_
- .Net与JS时间日期格式的转换问题对比分析_实用技巧_
- .NET 缓存模块设计实践_实用技巧_
- ASP.NET连接 Access数据库的几种方法_实用技巧_
- ASP.net如何连接SQL SERVER 2012数据库_实用技巧_
- Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件_实用技巧_
- ASP.NET实现大文件上传功能_实用技巧_
- 一句话轻松搞定asp.net分页_实用技巧_
- ASP.NET MVC5验证系列之Remote Validation_实用技巧_
- 解决VS2015中没有报表项(ReportViewer)的方法_实用技巧_
