创建Dictionary的几种方法

 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 Console.ReadKey();

 

posted @ 2018-08-22 11:22  魏兀平  阅读(1937)  评论(0编辑  收藏  举报