您当前的位置: 首页 > 网站编程 > 点NET教程 > ASP.NET中Dictionary的基本用法

ASP.NET中Dictionary的基本用法

作者:xiaoxiao 来源:未知 发布时间: 2014-07-30 11:43 点击:
//Dictionary位于System.Collections.Generic命名空间之下 /* *使用Dictionary之前必须引用System.Collections.Generic命名空间; *使用Dictionary时必须声明其键和值的数据类型(可以为任意类型); */ //声明实例化Dictionary为dic System.Collections.Generic. Dict

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中Dictionary的基本用法"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4