Dictionary 類別的使用

 

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Dictionary<string, string> result = new Dictionary<string, string>();
                //Dictionary<string, string, string> b = new Dictionary<string, string, string>();

                result.Add("key1", "value1");
                result.Add("kEy1", "value1");

                result.Add("key2", "value2");
                //result.Add("key2", "x");
                result.Add("key3", "value3");
                result.Add("key4", "value4");
                result.Add("key5", "value5");

                foreach (var item in result)
                {
                    Console.WriteLine("key = {0}, value = {1}", item.Key, item.Value);
                }

                Console.WriteLine("key1 = " + result["key1"]);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }
        }
    }
}

特性與說明如下:

1、Dictionary型別是由Key與Value所組成的。

2、Dictionary是IEnumerable型別的一種,故擁有IEnumerable與集合的特性。

3、Dictionary只接受Dictionary<TKey, TValue>型式,沒有這種Dictionary<TKey, TValue, TValue>型式的,如

Dictionary<string, string, string> b = new Dictionary<string, string, string>();

4、Dictionary的key區分大小寫,例如key為 "key1"與"kEy1"兩種key是不相等的。

5、Dictionary的key不能重複,value則無此限制。

6、由於Dictionary是集合的一種,所以也可以用foreach去一一巡灠他。

7、也可以個別指定特定的key去取值出來。

 

補充,Dictionary裡的value不一定要為string,也可以是陣列、物件

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Dictionary<string, string[]> result2 = new Dictionary<string, string[]>();

                result2.Add("key1", new string[] { "a", "b" });
                result2.Add("key2", new string[] { "c", "d" });

                foreach (var item in result2)
                {
                    Console.WriteLine("key = {0}, value[0] = {1}, value[1] = {2}", item.Key, item.Value[0], item.Value[1]);
                }

                Console.WriteLine("key1 first index = " + result2["key1"][0]);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }
        }
    }
}

VB.NET範例

Module Module1

    Sub Main()
        Dim result As New Dictionary(Of String, String())
        result("key1") = {"1", "2"}
        result("key2") = {"3", "4"}
        result.Add("key3", {"5", "6"})

        For Each item In result
            Console.WriteLine("key1 = {0}, value[0] = {1}, value[1] = {2}", item.Key, item.Value(0), item.Value(1))
        Next

        Console.WriteLine("key3 second index = " + result("key3")(1))
        Console.ReadKey()
    End Sub

End Module

 

參考資料:

Dictionary<TKey, TValue> 類別