集合——Dictionary<K,V>
Dictionary 泛型類有兩個類型參數,而 List 中只有一個。Dirctionary<K,V>,其中 K 代表字典中鍵個類型,V 代表字典中值的類型。字典中用一個鍵對應一個值,鍵和值存在這種一一映射的關係,檢索速度取決於爲 K 指定的類型的哈希算法的質量。常用的方法與 List 類似,但是需要分別使用 Keys 屬性和 Values 屬性單獨枚舉鍵和值。
Dictionary 成員參見: msdn.microsoft.com/zh-cn/library/3eayzh46(VS.80).aspx
看下面的例子:
- using System;
- using System.Collections.Generic;
- public class Example
- {
- public static void Main()
- {
- // 使用 string 類型建立字典的鍵和值
- Dictionary<string, string> openWith =
- new Dictionary<string, string>();
- // 添加一些元素給鍵和值,鍵是不可重複的,而值可以重複。
- openWith.Add("txt", "notepad.exe");
- openWith.Add("bmp", "paint.exe");
- openWith.Add("dib", "paint.exe");
- openWith.Add("rtf", "wordpad.exe");
- // 捕獲字典中存在相同的鍵而產生的異常
- try
- {
- openWith.Add("txt", "winword.exe");
- }
- catch (ArgumentException)
- {
- Console.WriteLine("An element with Key = \"txt\" already exists.");
- }
- // 可以使用鍵名作爲索引取出對應的值
- Console.WriteLine("For key = \"rtf\", value = {0}.",
- openWith["rtf"]);
- // 也可以通過鍵名索引改變對應的值
- openWith["rtf"] = "winword.exe";
- Console.WriteLine("For key = \"rtf\", value = {0}.",
- openWith["rtf"]);
- // 如果鍵名是不存在的,將會自動添加到字典
- openWith["doc"] = "winword.exe";
- // 捕獲不存在的鍵名
- try
- {
- Console.WriteLine("For key = \"tif\", value = {0}.",
- openWith["tif"]);
- }
- catch (KeyNotFoundException)
- {
- Console.WriteLine("Key = \"tif\" is not found.");
- }
- // 用TryGetValue方法測試一個鍵是否存在,若存在就顯示出來
- 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方法
- if (!openWith.ContainsKey("ht"))
- {
- openWith.Add("ht", "hypertrm.exe");
- Console.WriteLine("Value added for key = \"ht\": {0}",
- openWith["ht"]);
- }
- // 使用foreach加KeyValuePair方法得到所有元素
- Console.WriteLine();
- foreach( KeyValuePair<string, string> kvp in openWith )
- {
- Console.WriteLine("Key = {0}, Value = {1}",
- kvp.Key, kvp.Value);
- }
- // 獲得值集合
- Dictionary<string, string>.ValueCollection valueColl =
- openWith.Values;
- // 使用foreach枚舉所有值
- Console.WriteLine();
- foreach( string s in valueColl )
- {
- Console.WriteLine("Value = {0}", s);
- }
- // 獲得鍵集合
- Dictionary<string, string>.KeyCollection keyColl =
- openWith.Keys;
- // 使用foreach枚舉所有鍵
- Console.WriteLine();
- foreach( string s in keyColl )
- {
- Console.WriteLine("Key = {0}", s);
- }
- // Remove方法刪除元素
- Console.WriteLine("\nRemove(\"doc\")");
- openWith.Remove("doc");
- if (!openWith.ContainsKey("doc"))
- {
- Console.WriteLine("Key \"doc\" is not found.");
- }
- Console.Write("\nPress any key to exit...");
- Console.ReadKey();
- }
- }
- /* 運行結果:
- An element with Key = "txt" already exists.
- For key = "rtf", value = wordpad.exe.
- For key = "rtf", value = winword.exe.
- Key = "tif" is not found.
- Key = "tif" is not found.
- Value added for key = "ht": hypertrm.exe
- Key = txt, Value = notepad.exe
- Key = bmp, Value = paint.exe
- Key = dib, Value = paint.exe
- Key = rtf, Value = winword.exe
- Key = doc, Value = winword.exe
- Key = ht, Value = hypertrm.exe
- Value = notepad.exe
- Value = paint.exe
- Value = paint.exe
- Value = winword.exe
- Value = winword.exe
- Value = hypertrm.exe
- Key = txt
- Key = bmp
- Key = dib
- Key = rtf
- Key = doc
- Key = ht
- Remove("doc")
- Key "doc" is not found.
- */