txw1958 的 原文

说明
    必须包含名空间System.Collection.Generic
    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
    键必须是唯一的,而值不需要唯一的
    键和值都可以是任何类型(比如:string, int, 自定义类型,等等)
    通过一个键读取一个值的时间是接近O(1)
    键值对之间的偏序可以不定义

使用方法

    //定义
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");
    //取值
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
    //更改值
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
    //遍历key
foreach (string key in openWith.Keys)
{
Console.WriteLine("Key = {0}", key);
}
    //遍历value
foreach (string value in openWith.Values)
{
Console.WriteLine("value = {0}", value);
} //遍历value, Second Method
Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
foreach (string s in valueColl)
{
Console.WriteLine("Second Method, Value = {0}", s);
}
    //遍历字典
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
    //添加存在的元素
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
    //删除元素
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
    //判断键存在
if (openWith.ContainsKey("bmp")) // True
{
Console.WriteLine("An element with Key = \"bmp\" exists.");
}

参数为其它类型

    //参数为其它类型
Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
OtherType.Add(1, "1,11,111".Split(','));
OtherType.Add(2, "2,22,222".Split(','));
Console.WriteLine(OtherType[1][2]);

参数为自定义类型

首先定义类

    class DouCube
{
public int Code { get { return _Code; } set { _Code = value; } } private int _Code;
public string Page { get { return _Page; } set { _Page = value; } } private string _Page;
}

然后

    //声明并添加元素
Dictionary<int, DouCube> MyType = new Dictionary<int, DouCube>();
for (int i = 1; i <= 9; i++)
{
DouCube element = new DouCube();
element.Code = i * 100;
element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
MyType.Add(i, element);
}
    //遍历元素
foreach (KeyValuePair<int, DouCube> kvp in MyType)
{
Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
}

常用属性

名称    说明
    Comparer     获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
    Count        获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目。
    Item         获取或设置与指定的键相关联的值。
    Keys         获取包含 Dictionary<TKey, TValue> 中的键的集合。
    Values       获取包含 Dictionary<TKey, TValue> 中的值的集合。

常用方法
    名称    说明
    Add                 将指定的键和值添加到字典中。
    Clear               从 Dictionary<TKey, TValue> 中移除所有的键和值。
    ContainsKey         确定 Dictionary<TKey, TValue> 是否包含指定的键。
    ContainsValue       确定 Dictionary<TKey, TValue> 是否包含特定值。
    Equals(Object)      确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
    Finalize            允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
    GetEnumerator       返回循环访问 Dictionary<TKey, TValue> 的枚举器。
    GetHashCode         用作特定类型的哈希函数。 (继承自 Object。)
    GetObjectData       实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> 实例所需的数据。
    GetType             获取当前实例的 Type。 (继承自 Object。)
    MemberwiseClone     创建当前 Object 的浅表副本。 (继承自 Object。)
    OnDeserialization    实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
    Remove              从 Dictionary<TKey, TValue> 中移除所指定的键的值。
    ToString            返回表示当前对象的字符串。 (继承自 Object。)
    TryGetValue         获取与指定的键相关联的值。

[转] C#中的Dictionary的使用的更多相关文章

  1. JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)

    String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...

  2. C#中的Dictionary简介

    简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...

  3. VB中的Dictionary对象

    VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...

  4. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  5. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...

  6. C#中的Dictionary字典类介绍

      Dictionary字典类介绍 必须包含名空间System.Collection.Generic    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)    键必须是 ...

  7. C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈

    一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...

  8. C#中的Dictionary类,默认key是区分大小写的

    在C#中定义一个Dictionary Dictionary<string,string> dictionary = new Dictionary<string,string>( ...

  9. Python中的Dictionary

    Dictionary的创建 1 字面量 >>>D = {'a': 1, 'b': 2} >>>D {'b': 2, 'a': 1} 2 keyword参数 > ...

随机推荐

  1. 图解TCP/IP读书笔记(一)

    图解TCP/IP读书笔记(一) 第一章 网络基础知识 本学期的信安概论课程中有大量的网络知识,其中TCP/IP占了相当大的比重,让我对上学期没有好好学习计算机网络这门课程深感后悔.在老师的推荐下开始阅 ...

  2. centos磁盘爆满,查找大文件并清理

    今天发现vps敲入crontab -e 居然提示 “Disk quota exceeded” 无法编辑.于是"df -h"查了查发现系统磁盘空间使用100%了.最后定位到是/var ...

  3. android 广播的使用

    在Activity中,注册广播的一个Demo. 总共分3步 第一步:定义一个BroadcastReceiver广播接收类: private BroadcastReceiver mBroadcastRe ...

  4. spring路径通配符

    在应用Spring的工程中,使用class path的方式加载配置文件应该是最常用的做法,然而对大部分人来说,刚开始使用Spring时,几乎都碰到过加载配置文件失败的情况,除了配置上的错误外,很多时候 ...

  5. Android:在eclipse中快速多行注释的方法

    http://blog.csdn.net/jianghuiquan/article/details/8534337 也许你能够记住以下部分快捷键,对你开发和设计过程中大裨益! 1.//注释添加和取消 ...

  6. C# 值类型和引用类型的区别

    C#  值类型和引用类型的区别 1. 值类型的数据存储在内存的栈中:引用类型的数据存储在内存的堆中,而内存单元中只存放堆中对象的地址. 2. 值类型存取速度快,引用类型存取速度慢. 3. 值类型表示实 ...

  7. CMD下查看某个端口被谁占用了

    cmd运行 netstat -aon|findstr "8080" 运行结果如下 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 6416 TCP [:: ...

  8. jQuery_添加与删除元素

    一.jQuery添加元素(通过 jQuery,可以很容易地添加新元素/内容.) 1.添加新的 HTML 内容,用于添加新内容的四个 jQuery 方法(都能解析HTML标签): append() - ...

  9. 关于Qt

    什么是Qt Qt是一个针对桌面.嵌入式.移动设备的一个跨平台的应用程序开发框架,支持的平台包括Linux.OS X.Windows.VxWorks.QNX.Android.iOS.BlackBerry ...

  10. 51nod1376 最长递增子序列的数量

    O(n2)显然超时.网上找的题解都是用奇怪的姿势写看不懂TAT.然后自己YY.要求a[i]之前最大的是多少且最大的有多少个.那么线段树维护两个值,一个是当前区间的最大值一个是当前区间最大值的数量那么我 ...