本文实例讲述了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/>");

最新文章

  1. 【13-Annotation】
  2. CXF集成spring做webservice接口
  3. Hibernate之管理session与批处理
  4. C# Attribute
  5. Fedora 22(15以上版本)开机自启动脚本
  6. [google面试CTCI] 1-5.替换字符串中特定字符
  7. Qt中的串口编程之三
  8. jsp的验证码实现
  9. selenium元素定位不到之iframe
  10. bzoj:1681 [Usaco2005 Mar]Checking an Alibi 不在场的证明
  11. 【微信小程序】对微信http请求API的封装,方便对错误码进行处理
  12. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
  13. TLS握手、中断恢复与证书中心的原因
  14. ajax解决跨域
  15. getent passwd 不能访问到 ldap 的用户
  16. logstash启动脚本
  17. C#-VS字符串、日期、时间和时间段
  18. BitAdminCore框架更新日志20180523
  19. django url 路由设置技巧
  20. 快速稀疏角点光流框架(Fast sparse corner optical flow framework)

热门文章

  1. [读后感]spring Mvc 教程框架实例以及系统演示下载
  2. Spring-1 之入门
  3. 001-使用idea开发环境安装部署,npm工具栏,脚本运行
  4. Linux-profile、bashrc、bash_profile之间的区别和联系
  5. 人工智能 VS 机器学习 VS 深度学习
  6. ajax跨域--jsop方法
  7. mongodb - Replication Set搭建过程
  8. .Net程序员面试 中级篇 (回答Scott Hanselman的问题)
  9. ubuntu:好用的help命令
  10. 浅谈iOS 5的StoryBoard