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 实体的状态变化

    阅读目录: 1. 应用场景 2. 场景测试 3. 问题分析 4. 追根溯源 5. 简要总结 1. 应用场景 首先,应用程序使用 EntityFramework,应用场景中有两个实体 S_Class(班 ...

  2. swift 中指针的使用UnsafeMutablePointer

    在swift中已经弱化了指针的使用,可以这么使用 let s: NSRange = NSMakeRange(, ) let at = UnsafeMutablePointer<NSRange&g ...

  3. VMware网络设置详解--不错

    我们知道,VMware Workstation提供了很多虚拟设备,利用这些设备,我们除了可以组建典型的桥接网络.仅主机网络.NAT网络外,还能组建复杂的自定义网络.本篇 目的就是让大家认识和掌握VMw ...

  4. 移动端 h5调试技巧

    一 安卓 一 chrome 1.安卓手机安装chrome浏览器,手机打开开发者模式,用usb线链接电脑,并且允许调试. 2.电脑chrome地址栏输入 chrome://inspect 进入后点击 i ...

  5. 【集合框架】JDK1.8源码分析之Collections && Arrays(十)

    一.前言 整个集合框架的常用类我们已经分析完成了,但是还有两个工具类我们还没有进行分析.可以说,这两个工具类对于我们操作集合时相当有用,下面进行分析. 二.Collections源码分析 2.1 类的 ...

  6. Java 技能树

  7. SQL Server安全(11/11):审核(Auditing)

    在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...

  8. React Native Changed the World? or Nothing.

    RN是一个awesome的技术, facebook很有想法的团队创造出一项新的技术改变了native开发界. 但是RN本身又疑点重重, RN是为了解决什么问题而存在的? 在诞生了一年后, RN又解决了 ...

  9. iOS9的几个新关键字(nonnull、nullable、null_resettable、__null_unspecified)

    1.nonnull:字面意思就能知道:不能为空(用来修饰属性,或者方法的参数,方法的返回值) 代码: //三种使用方式都可以 @property (nonatomic, copy, nonnull) ...

  10. 五小步让VS Code支持AngularJS智能提示

    本文想通过配置VS Code来实现对AngularJS的智能提示.在一般的情况下对于在HTML页面是支持提示的.但是在js页面就不是很友好,它是记忆你之前的输入,要是之后有重复的输入,VS Code会 ...