集合——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.
- */