栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

//Dictionary位于System.Collections.Generic命名空间之下

//声明实例化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中的键和值:
"); //循环获取dic中的键和值 foreach (KeyValuePair keyDic 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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56130.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号