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.Object 
  System.Collections.Generic.Dictionary

Dictionary摘抄的更多相关文章

  1. 年度巨献-WPF项目开发过程中WPF小知识点汇总(原创+摘抄)

    WPF中Style的使用 Styel在英文中解释为”样式“,在Web开发中,css为层叠样式表,自从.net3.0推出WPF以来,WPF也有样式一说,通过设置样式,使其WPF控件外观更加美化同时减少了 ...

  2. .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? ...

  3. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  4. ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary

    AR.Global 文档 1:对象或属性: 名称 类型 说明 DG 对象 DataGrid操作对象 //datagrid集合,根据ID取出DataGrid对象,将Json当数组用. Items: ne ...

  5. WebAPI接口返回ArrayList包含Dictionary对象正确解析

    一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...

  6. Linq在Array,List,Dictionary中的应用

    Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...

  7. python之最强王者(8)——字典(dictionary)

    1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...

  8. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  9. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

随机推荐

  1. EntityFramework 7 Left Join Where is error(Test record)

    First of all, my English is very poor, so I may not be a very good expression, very sorry! In this b ...

  2. mysql表名查询sql

    select table_schema,table_name,engine from information_schema.tables where table_schema not in('info ...

  3. 浅析.NET的反射特性

    在.net框架体系内,反射特性较多的应用到.反射的相关定义分为两种. 自然解释:射是一种自然现象,表现为受刺激物对刺激物的逆反应:这是反射的字面解释,我们看一下计算机编程中的反射: 编程解释:通过 S ...

  4. NSPredicate 的使用

    NSPredicate是什么? NSPredicate 是预测的意思 但是我们常翻译成谓词 它可以干什么? 使用NSPredicate可以定义模糊查询条件 根据一定的条件 我们就可以从一个数组中快速找 ...

  5. Effective java笔记(四),泛型

    泛型为集合提供了编译时类型检查. 23.不要在代码中使用原生态类型 声明中具有一个或多个类型参数的类或接口统称为泛型.List<E>是一个参数化类,表示元素类型为E的列表.为了提供兼容性, ...

  6. 自己实现简单的AOP(五)使Demo适应webApi、亦可完成属性自动注入

    在前文的Demo中,webApi的Controller是不能自动注入的,原因是 IHttpController 和 IController 是通过两个不同的途径进行激活的. IHttpControll ...

  7. jQuery美女幻灯相册轮播源代码

    体验效果:http://hovertree.com/texiao/jquery/ 本幻灯片包含小图列表和大图轮播,包含图片标题和详细介绍,详细介绍字数可以很多,每张图片包含链接,可以实现跳转 HTML ...

  8. c#中奖算法的实现

    算法名称 Alias Method public class AliasMethod { /* The probability and alias tables. */ private int[] _ ...

  9. HTML、CSS、JavaScript和PHP的注释

  10. 转载:C#中的泛型

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...