Dictionary摘抄
Dictionary,字典,键值对集合。
下面的代码示例创建一个空的带有字符串键的字符串 Dictionary,并使用 Add 方法添加一些元素。该示例演示在尝试添加重复的键时 Add 方法引发ArgumentException。
该示例使用 Item 属性(在 C# 中为 索引器)来检索值,演示当请求的键不存在时将引发 KeyNotFoundException,并演示与键相关联的值可被替换。
该示例演示当程序必须经常尝试字典中不存在的键值时,如何使用 TryGetValue 方法作为一种更有效的方法来检索值,它还演示如何使用ContainsKey 方法在调用 Add 方法之前测试某个键是否存在。
该示例演示如何枚举字典中的键和值,以及如何分别使用 Keys 属性和 Values 属性来单独枚举键和值。
最后,该示例演示 Remove 方法。
using System;
using System.Collections.Generic; public class Example
{
public static void Main()
{
//创建一个字典,键是字符串类型,值是字符串类型。
Dictionary<string, string> openWith =new Dictionary<string, string>(); //在字典中添加一些元素。不能有重复的键,可以有重复的值。
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe"); // 如果新键已经在字典中,则添加方法将引发异常。
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("该键名已经在字典中存在");
} // 可以直接通过:字典对象名["键名称"] 访问该键对应的值。
Console.WriteLine(" value = {0}", openWith["rtf"]); // 可以通过给键重新赋值来改变键的值
openWith["rtf"] = "winword.exe";
Console.WriteLine(" key = \"rtf\", value = {0}.",openWith["rtf"]); // 如果键名在字典集合中不存在,赋值时就新建。
openWith["doc"] = "winword.exe";
Console.WriteLine(" key = \"doc\",value ={0}", openWith["doc"]); // 如果请求的键不在字典中,则抛出异常。
try
{
Console.WriteLine("For key = \"tif\", value = {0}.",openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
} //当一个程序经常要获取键对应的值时,用TryGetValue获取与指定的键相关联的值效率高。
string value = "";
if (openWith.TryGetValue("txt", out value))
{
Console.WriteLine("For key = \"txt\", value = {0}.", value);
}
else
{
Console.WriteLine("Key = \"tif\" is not found.");
} // 用ContainsKey测试是否包含指定的键
if (!openWith.ContainsKey("ht"))
{
Console.WriteLine("不包含,我想创建");
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",openWith["ht"]);
} //当使用foreach遍历字典中元素时,元素检索KeyValuePair对象
foreach (KeyValuePair<string, string> kvp in openWith) {
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
//普通遍历
//foreach (var kvp in openWith)
//{
// Console.WriteLine("Key = {0}, Value = {1}",
// kvp.Key, kvp.Value);
//} //要获得单独的值,使用值属性
Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
Console.WriteLine();//输出空行
//遍历输出,注意valueColl是强类型,要使用匹配的类型。 使用var未定义类型也可以输出。
foreach (string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
} // 要获得单独的键,使用键属性
Dictionary<string, string>.KeyCollection keyColl =openWith.Keys;
Console.WriteLine();
//遍历输出
foreach (string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
} // 使用Remove方法移除键值对
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc"); if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
} Console.ReadKey();
}
}
System.Collections.Generic.Dictionary
Dictionary摘抄的更多相关文章
- 年度巨献-WPF项目开发过程中WPF小知识点汇总(原创+摘抄)
WPF中Style的使用 Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了 ...
- .Net 中HashTable,HashMap 和 Dictionary<key,value> 和List<T>和DataTable的比较
参考资料 http://www.cnblogs.com/MichaelYin/archive/2011/02/14/1954724.html http://zhidao.baidu.com/link? ...
- C#数组,List,Dictionary的相互转换
本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...
- ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary
AR.Global 文档 1:对象或属性: 名称 类型 说明 DG 对象 DataGrid操作对象 //datagrid集合,根据ID取出DataGrid对象,将Json当数组用. Items: ne ...
- WebAPI接口返回ArrayList包含Dictionary对象正确解析
一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...
- Linq在Array,List,Dictionary中的应用
Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...
- python之最强王者(8)——字典(dictionary)
1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...
- Swift3 - String 字符串、Array 数组、Dictionary 字典的使用
Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...
- [LeetCode] Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
随机推荐
- linux dd命令详解
Linux-dd命令详解 dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 名称: dd 使用权限: 所有使用者dd 这个指令在 ...
- 相克军_Oracle体系_随堂笔记007-PGA
实际工作中,Oracle中有两个很重要:Server Process 和 PGA. PGA内存作用和构成 1.PGA作用 2.PGA构成 1)private SQL area 2)Sess ...
- T-SQL简单查询语句
简单查询: 1.最简单查询(查所有数据)select * from 表名: 注:* 代表所有列select * from info 2.查询指定列select code,name from info ...
- RichTextBox实现鼠标右键(剪切,复制,粘贴)功能
private static void InitRichTextBoxContextMenu(RichTextBox textBox) { //创建剪切子菜单 var cutMenuItem = ne ...
- 【406错误】 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
今天遇到一个奇怪的错误,关于Springmvc的,我明明在Controller方法中写了@ResponseBody,返回一个Map,结果报了406错误. 结果发现,少了一个jar包: 加上去就没事了.
- 12款响应式 Lightbox(灯箱)效果插件
灯箱效果(Lightbox)是网站中最常用的效果之一,用于实现类似模态对话框的效果.网络上各种 Lightbox 插件琳琅满目,随着响应式设计(Respnsive Design)的发展,这一先进理念也 ...
- FreeRTOS 中断优先级嵌套错误引发HardFault异常解决(转)
最近在使用FreeRTOS的时候,突然发现程序在运行了几分钟之后所有的任务都不再调用了,只有几个中断能正常使用,看来是系统挂掉了,连续测试了几次想找出问题,可是这个真的有点不知所措. 我 ...
- Win 10 UWP开发系列:设置AppBarButton的图标
在WP8以前,页面最下面的四个小圆按钮是不支持绑定的,WP8.1 RT之后,系统按钮升级成了AppBarButton,并且支持绑定了.在Win10 UWP开发中,按钮的样式发生了变化,外面的圆圈没有了 ...
- 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox
[源码下载] 背水一战 Windows 10 (28) - 控件(文本类): TextBox, PasswordBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) T ...
- centos下安装php扩展php-memcached
说来坎坷,为了安装这个php的扩展php-memcached,连操作系统都换了,从centos5.5升级到了centos6.8!! centos5.5中在安装php扩展php-memcached的依赖 ...