DESCRIPTIONは、
ネームスペースが含まれている必要がありSystem.Collection.Generic
(二つの要素から構成される:キーと値)キーと値のペアであり、各要素辞書
キーは、一意である必要があり、値の必要性は、
キーと値とすることができます(例:文字列、整数、カスタムタイプ、等)は、任意のタイプのものとすることが
近い時間O(1)のキー値によって読み出される
定義されていてもよい、キーと値のペアの間の半順序
用途:
1 // 定义
2 辞書< 文字列、文字列 > openWith = 新しい辞書< 文字列、文字列 > ();
3
4
5 // 添加元素
6 openWith.Add(" TXT "、" メモ帳" )。
7 openWith.Add(" BMP "、" paint.exe " )。
8 openWith.Add(" DIB "、「ペイント。値= {0}。"
、openWith [ " RTF " ])。
19
20
21 // 遍历キー
22 のforeach(文字列キーでopenWith.Keys)
23 {
24 Console.WriteLineを(" キー= {0} " 、キー)
25 }
26
27
28 29 // 遍历値
30 のforeach(文字列値にopenWith.Values)
31 {
32 Console.WriteLineを(" 値= {0} "、値)。
33 }
34
35 // 遍历値、第2の方法
36 辞書< 文字列、文字列 > .ValueCollection valueColl = openWith.Values。
37 foreachの(ストリング S でvalueColl)
38 {
39 Console.WriteLineを(" 第2の方法、値= {0} " 、S)。
40 }
41 42
43
44 // 遍历字典
45 のforeach(KeyValuePair < ストリング、ストリング>のkVp においてopenWith)
46 {
47 Console.WriteLineを(" キー= {0}、値= {1} " 、kvp.Key、kvp.Value)。
48 }
49
50
51 52 // 添加存在的元素
53 トライ
54 {
55 openWith.Add(" TXT "、" WINWORD.EXE " )。
56 }
57 キャッチ(ArgumentExceptionが)
58 {
59 Console.WriteLineを("キーを持つ要素= \「TXTは、\」は既に存在します。" );
60 }
61 62
63
64 65 // 删除元素
66 openWith.Remove(" DOC " )、
67 であれば(openWith.ContainsKey(!" DOC " ))
68 {
69 Console.WriteLineを(" キー\ "DOC \ "が見つかりません。" );
70 }
71 72
73
74 // 判断键存在
75 場合(openWith.ContainsKey(""))// 真の
76 {
77 Console.WriteLineを(" キー= \ "BMP \"の要素が存在する。" );
78 }
パラメータの他のタイプ
1 //参数为其它类型
2 Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
3 OtherType.Add(1, "1,11,111".Split(','));
4 OtherType.Add(2, "2,22,222".Split(','));
5 Console.WriteLine(OtherType[1][2]);
参数为自定义类型
首先定义类
1 class DouCube
2 {
3 public int Code { get { return _Code; } set { _Code = value; } } private int _Code;
4 public string Page { get { return _Page; } set { _Page = value; } } private string _Page;
5 }
然后
1 //声明并添加元素
2 Dictionary<int, DouCube> MyType = new Dictionary<int, DouCube>();
3 for (int i = 1; i <= 9; i++)
4 {
5 DouCube element = new DouCube();
6 element.Code = i * 100;
7 element.Page = "http://www.doucube.com/" + i.ToString() + ".html";
8 MyType.Add(i, element);
9 }
10 11 //遍历元素
12 foreach (KeyValuePair<int, DouCube> kvp in MyType)
13 {
14 Console.WriteLine("Index {0} Code:{1} Page:{2}", kvp.Key, kvp.Value.Code, kvp.Value.Page);
15 }
常用属性
名称 说明
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 获取与指定的键相关联的值。