摘要:Dictionary 泛型類別
這個類別是 .NET Framework 2.0 版的新功能。
表示索引鍵和值的集合。
命名空間: System.Collections.Generic
Dictionary<string, string> openWith = new Dictionary<string, string>();
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
openWith["doc"] = "winword.exe";
openWith["rtf"]
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);
}
資料來源1: http://msdn.microsoft.com/zh-tw/library/xfhwa508(VS.80).aspx
資料來源2 (List 與 Dictionary 的搜尋差異度)