- 接口描述
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> 类型。
代码案例如下:
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
// 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();