您好,登錄后才能下訂單哦!
接口描述
Represents a nongeneric collection of key/value pairs.[代表一個非泛型的鍵/值對的集合]。在System.Collections.Generic包下面。所在程序集為mscorlib.dll中。
語法
代碼案例如下:
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();
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。