Dictionary 泛型類別

摘要: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 的搜尋差異度)

http://columns.chicken-house.net/post/e8a9b2e5a682e4bd95e5adb8e5a5bd-e5afabe7a88be5bc8f-2-e782bae4bb80e9babc-programmer-e8a9b2e5adb8e8b387e69699e7b590e6a78b-.aspx