Dictionary<,>

    1. using System;
    2. using System.Collections.Generic;
    3. namespace _076_字典集合
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. //Dictionary<TKey,TValue> 指定类型的键、值
    10. Dictionary<int, string> dic = new Dictionary<int, string>();
    11. dic.Add(1, "卡特琳娜");
    12. dic.Add(2, "阿卡丽");
    13. dic.Add(3, "瑞雯");
    14. //dic.Add(1, "会异常");
    15. //dic[1] = "不会异常";
    16. foreach (var item in dic)
    17. {
    18. Console.WriteLine(item);
    19. }
    20. Console.WriteLine("=========================================");
    21. foreach (KeyValuePair<int, string> kv in dic)
    22. {
    23. Console.WriteLine("键:{0},值:{1}", kv.Key, kv.Value);
    24. }
    25. Console.WriteLine("=========================================");
    26. foreach (var item in dic.Keys)
    27. {
    28. Console.WriteLine("键:{0},值:{1}", item, dic[item]);
    29. }
    30. Console.WriteLine("=========================================");
    31. Console.ReadKey();
    32. }
    33. }
    34. }