创建Dictionary的几种方法
原创
©著作权归作者所有:来自51CTO博客作者魏兀平的博客的原创作品,请联系作者获取转载授权,否则将追究法律责任
1 //Lued
2 //创建的时候赋值
3 Dictionary<int, string> dict1 = new Dictionary<int, string>() { { 0, "fuck!" }, { 1, "fuck the world!" } };
4 Console.WriteLine("dcit1:");
5 foreach (var dict in dict1) {
6 Console.WriteLine(dict.Key +"---"+dict.Value);
7 }
8 Console.WriteLine();
9 //用Add方法
10 Dictionary<int, string> dict2 = new Dictionary<int, string>();
11 dict2.Add(0, "fuck!");
12 dict2.Add(1, "fuck the world!");
13 Console.WriteLine("dcit2:");
14 foreach (var dict in dict2)
15 {
16 Console.WriteLine(dict.Key + "---" + dict.Value);
17 }
18 Console.WriteLine();
19 //用[]下标类似于数组的方法
20 Dictionary<int, string> dict3 = new Dictionary<int, string>();
21 dict3[0] = "fuck!";
22 dict3[1] = "fuck the world!";
23 Console.WriteLine("dcit3:");
24 foreach (var dict in dict3)
25 {
26 Console.WriteLine(dict.Key + "---" + dict.Value);
27 }
28 Console.WriteLine();
29 Console.WriteLine("ok!");
30