-
接口描述
Represents a nongeneric collection of key/value pairs.[代表一个非泛型的键/值对的集合]。在System.Collections.Generic包下面。所在程序集为mscorlib.dll中。
-
语法
public Interface IDictionary<TKey,TValue>
:ICollection<KeyValuePair<TKey,TValue>>,IEnumerable<KeyValuePair<TKey,TValue>>,IEnumberable
备注 IDictionary<TKey, TValue> 接口是键/值对的泛型集合的基接口。每个元素都是一个存储在 KeyValuePair<TKey, TValue> 对象中的键/值对。每一对都必须有唯一的键。 实现在是否允许 key 为 null 方面有所不同。 此值可以为 null,并且不必是唯一的。 IDictionary<TKey, TValue> 接口允许对所包含的键和值进行枚举,但这并不意味着任何特定的排序顺序。C# 语言中的 foreach 语句(在 Visual Basic 中为 For Each,在 C++ 中为 for each)需要集合中每个元素的类型。 由于 IDictionary<TKey, TValue> 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。 而是 KeyValuePair<TKey, TValue> 类型。
1
2
3
4
|
foreach
(KeyValuePair<
int
,
string
> kvp
in
myDictionary)
{
Console.WriteLine(
"Key = {0}, Value = {1}"
, kvp.Key, kvp.Value);
}
|
方法名 | 方法描述 |
Add(T) | 将某项添加到 ICollection<T> 中。 (继承自 ICollection<T>。) |
Add(TKey,TValue) | 在 IDictionary<TKey, TValue> 中添加一个带有所提供的键和值的元素。 |
Clear() | 清空ICollection<T>中的所有元素。 |
Contains | 确认ICollection<T>集合中是否有特定的值 |
ContainsKey | 确认IDictionary<TKey, TValue>集合中是否包含指定键元素。 |
CopyTo | 从特定的 Array 索引开始,将 ICollection<T> 的元素复制到一个 Array 中。 (继承自 ICollection<T>。) |
GeEnumurator | 返回一个循环访问集合的枚举器。 (继承自 IEnumerable<T>。) |
Remove(T) | 移除指定元素 |
Remove(TKey) | 移除指定键的元素 |
TryGetValue | 获得与指定键关联的元素值 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
// Create a new dictionary of strings, with string keys,
// and access it through the IDictionary generic interface.
IDictionary<
string
,
string
> openWith =
new
Dictionary<
string
,
string
>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add(
"txt"
,
"notepad.exe"
);
openWith.Add(
"bmp"
,
"paint.exe"
);
openWith.Add(
"dib"
,
"paint.exe"
);
openWith.Add(
"rtf"
,
"wordpad.exe"
);
// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
openWith.Add(
"txt"
,
"winword.exe"
);
}
catch
(ArgumentException)
{
Console.WriteLine(
"An element with Key = \"txt\" already exists."
);
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine(
"For key = \"rtf\", value = {0}."
,
openWith[
"rtf"
]);
// The indexer can be used to change the value associated
// with a key.
openWith[
"rtf"
] =
"winword.exe"
;
Console.WriteLine(
"For key = \"rtf\", value = {0}."
,
openWith[
"rtf"
]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith[
"doc"
] =
"winword.exe"
;
// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine(
"For key = \"tif\", value = {0}."
,
openWith[
"tif"
]);
}
catch
(KeyNotFoundException)
{
Console.WriteLine(
"Key = \"tif\" is not found."
);
}
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
string
value =
""
;
if
(openWith.TryGetValue(
"tif"
,
out
value))
{
Console.WriteLine(
"For key = \"tif\", value = {0}."
, value);
}
else
{
Console.WriteLine(
"Key = \"tif\" is not found."
);
}
// ContainsKey can be used to test keys before inserting
// them.
if
(!openWith.ContainsKey(
"ht"
))
{
openWith.Add(
"ht"
,
"hypertrm.exe"
);
Console.WriteLine(
"Value added for key = \"ht\": {0}"
,
openWith[
"ht"
]);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach
( KeyValuePair<
string
,
string
> kvp
in
openWith )
{
Console.WriteLine(
"Key = {0}, Value = {1}"
,
kvp.Key, kvp.Value);
}
// To get the values alone, use the Values property.
ICollection<
string
> icoll = openWith.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach
(
string
s
in
icoll )
{
Console.WriteLine(
"Value = {0}"
, s);
}
// To get the keys alone, use the Keys property.
icoll = openWith.Keys;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach
(
string
s
in
icoll )
{
Console.WriteLine(
"Key = {0}"
, s);
}
// Use the Remove method to remove a key/value pair.
Console.WriteLine(
"\nRemove(\"doc\")"
);
openWith.Remove(
"doc"
);
if
(!openWith.ContainsKey(
"doc"
))
{
Console.WriteLine(
"Key \"doc\" is not found."
);
}
Console.ReadLine();
|