Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1, "wjl"); d.Add(2, "zhangsan"); d.Add(3, "hyf"); //Dictionary里面可以添加不同键相同值 d.Add(4, "hyf"); //检测集合里面是否包含一个键 if (!d.ContainsKey(4)) { d.Add(5, "ljm"); } else { d[5] = "王狗蛋儿"; } //遍历字典的两种方式 foreach (int key in d.Keys) { Console.WriteLine("键:{0} 值:{1}", key,d[key]); } Console.WriteLine("********************************"); foreach (KeyValuePair<int,string> kv in d) { Console.WriteLine("键:{0} 值:{1}", kv.Key, kv.Value); }