IDictionary<TKey,TValue>数据字典使用讲解

  1. 接口描述

       Represents a nongeneric collection of key/value pairs.[代表一个非泛型的键/值对的集合]。在System.Collections.Generic包下面。所在程序集为mscorlib.dll中。

  2. 语法

   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);
}

    注:foreach 语句是对枚举数的包装,它只允许从集合中读取,不允许写入集合。


    方法列表:

方法名 方法描述
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获得与指定键关联的元素值

注:扩展方法可到官方MSDN查看:http://msdn.microsoft.com/zh-cn/library/8hyehyw5(v=vs.110).aspx

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  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  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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值