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

说明
    必须包含名空间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,11,111".Split(','));
OtherType.Add(, "2,22,222".Split(','));
Console.WriteLine(OtherType[][]);

参数为自定义类型

首先定义类

  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 = ; i <= ; i++)
{
DouCube element = new DouCube();
element.Code = i * ;
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. .NET中Dictionary<TKey, TValue>浅析

    .NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...

  2. AutoMapper之ABP项目中的使用介绍

    最近在研究ABP项目,昨天写了Castle Windsor常用介绍以及其在ABP项目的应用介绍 欢迎各位拍砖,有关ABP的介绍请看阳光铭睿 博客 AutoMapper只要用来数据转换,在园里已经有很多 ...

  3. C#中Dictionary<TKey,TValue>排序方式

    自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  4. iOS开发UI篇—iPad开发中得modal介绍

    iOS开发UI篇—iPad开发中得modal介绍 一.简单介绍 说明1: 在iPhone开发中,Modal是一种常见的切换控制器的方式 默认是从屏幕底部往上弹出,直到完全盖住后面的内容为止 说明2: ...

  5. C#中Dictionary小记

    使用C#中Dictionary的一下细节小记: 一.Dictionary.Add(key,value) 与 Dictionary[key]=value的区别: 如果Dictionary中已经有了key ...

  6. objective-c 中的关联介绍

    objective-c 中的关联介绍 转载请注明CSDN博客上的出处: http://blog.csdn.net/daiyibo123/article/details/46471993 如何设置关联 ...

  7. ORACLE 中的 锁 介绍

    ORACLE 中的 锁 介绍 Oracle数据库支持多个用户同时与数据库进行交互,每个用户都可以同时运行自己的事务,从而也需要对并发访问进行控制.Oracle也是用“锁”的机制来防止各个事务之间的相互 ...

  8. C#中Dictionary的用法

    在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...

  9. Android中Snackbar的介绍以及使用

    Android中Snackbar的介绍以及使用 介绍 Snackbar可以说是Toast的升级版,不仅有显示信息的功能,还可以添加一个Action,实现点击功能,可以右滑删除. 效果图 Snackba ...

随机推荐

  1. SQLServer之数据库行锁

    行锁使用注意事项 1.ROWLOCK行级锁确保在用户取得被更新的行,到该行进行更新,这段时间内不被其它用户所修改.因而行级锁即可保证数据的一致性,又能提高数据操作的并发性. 2.ROWLOCK告诉SQ ...

  2. 求出100以内的素数(java实现)

    j package test1; //2018/11/30 //求100以内的所有素数 public class Main10 { public static void main(String[] a ...

  3. .net prams关键字

    先举个例子: 代码如下: class Program { static void Main(string[] args) { Console.WriteLine(Sum(1)); Console.Wr ...

  4. Storm入门(二)集群环境安装

    1.集群规划 storm版本的变更:storm0.9.x   storm0.10.x  storm1.x上面这些版本里面storm的核心源码是由Java+clojule组成的.storm2.x后期这个 ...

  5. vue打包发布在spingboot项目中 vue-router路由的处理

    (原) 以下例子springboot后端地址为:localhost:7080/pingandai vue前端地址为:locahost:8080/pingandai/ 1.如果路由模式设置的是histo ...

  6. SQL insert into select 语句

    遇到权限数据变更的需要批量到别的平台, 在175平台添加一个权限需要, 批量到别的现有平台, 以后的建站, 会把sql放到自动建站里面; 权限的 insert into select 表一: `ous ...

  7. Luogu1574 超级数

    Luogu1574 超级数 \(n\) 次询问不超过 \(a_i\) 的最大反素数 \(n\leq10^5,\ a_i\leq10^{17}\) 数论 似乎重题 bzoj1053 [HAOI2007] ...

  8. 记一次CPU飙升BUG

    图文地址:https://mp.weixin.qq.com/s?__biz=Mzg3NjEzODQ4NQ==&mid=2247483690&idx=1&sn=7c926f400 ...

  9. ginput函数用法

    1.ginput函数:获取指定点坐标值 2.用法说明 (1)[x,y] = ginput(n) 函数从当前的坐标轴上选择n个点,并返回这n个点相应的坐标值(x,y).这n个点可由鼠标定位.用户可以按下 ...

  10. centos6.8 搭建postfix/dovecot邮件服务器

    postfix/dovecot邮件服务器 安装配置参考链接  http://www.cnblogs.com/jkklearn/p/7280045.html (domain 为自己域名 xxx.com) ...