✔ 最佳答案
using System;
using System.Collections.Generic;
/*
** Dictionary provides very fast LOOKUPS with keys to get values.
** It is used when you have MANY different elements and not limited to 6.
** The key value is unique key in the data structure.
*/
public class Program
{
public static void Main()
{
Dictionary<string, string> IDNumber = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
// Prefer...
if (!IDNumber.ContainsKey("4")) IDNumber.Add("4", "four");
if (!IDNumber.ContainsKey("4")) IDNumber.Add("4", "four"); // ignore
foreach(KeyValuePair<string, string> pair in IDNumber)
{
Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
2012-04-29 08:31:17 補充:
// Usage...
Console.WriteLine(IDNumber["4"]);