Dictionary 字典类型记录

2022-07-26 09:20:29 浏览数 (1)

Dictionary 是一个很好的类型,可以不断增加.例如:

代码语言:javascript复制
 Dictionary<string, string> data_str = new Dictionary<string, string>();
              data_str.Add("aaa","ccc");
              data_str.Add("BBB", "EEE");
              data_str.Add("CCC", "FFF");

也可以这种方式。。

代码语言:javascript复制
Dictionary<string, int> dic = new Dictionary<string, int>() {
    {"张三",1},
    {"李四",2},
};
string result = JsonConvert.SerializeObject(dic);
Console.WriteLine(result); //{"张三":1,"李四":2}

更可以这种方式:

代码语言:javascript复制
result = "{"张三":1,"李四":2}";
Dictionary<string, int> dic2 = JsonConvert.DeserializeObject<Dictionary<string, int>>(result);
foreach (var item in dic2)
{
    Console.WriteLine($"{item.Key}---->{item.Value}");
}

20220726补充

代码语言:javascript复制
   Dictionary<string, Dictionary<int, int>> dicTemp = new Dictionary<string, Dictionary<int, int>>();
       Dictionary<int, int> AB = new Dictionary<int, int>();
            AB.Add(1, 3);
            AB.Add(2, 4);
            AB.Add(3, 5);
  dicTemp.Add("pppp",AB);

 Dictionary<string, List<List_str>> dicList = new Dictionary<string, List<List_str>>();
    List<List_str> List_obj = new List<List_str>();
                   List_str List_arr = new List_str();
                   List_arr.Name = "Name01";
                   List_arr.Id = "G20220726";
                   List_arr.Value = "Test01";            
                   List_obj.Add(List_arr);
    dicList.Add("AAA", List_obj);

0 人点赞