C#基礎:泛型字典
阿新 • • 發佈:2019-02-11
- //Dictionary<K,V>示例程式碼
- public static void Main(string[] args){
- Dictionary<string, int> things = new Dictionary<string, int>();
- things.Add("Green Things", 29);
- things.Add("Blue Things", 94);
- things.Add("Yellow Things", 34);
- things.Add("Red Things", 52);
- things.Add("Brown Things", 27);
- foreach (KeyValuePair<string, int> item in things)//輸出key-value
- {
- Console.WriteLine(item.Key + " :" + item.Value);
- }
- Console.WriteLine();
- foreach (string item in things.Keys)//輸出key
- {
- Console.WriteLine(item);
- }
- Console.WriteLine();
- foreach (int item in things.Values)//輸出value
- {
- Console.WriteLine(item);
- }
- Console.WriteLine();
- }