如何存储字典集合
本文关键字:字典 集合 存储 何存储 | 更新日期: 2023-09-27 17:56:11
我需要在 C# 中创建各种数量的Dictionary<string, string>
;我该怎么做?
我试过使用List<Dictionary<string, string>>
,但这不起作用。
请参阅此示例:
List<Dictionary<string,string>> list = new List<Dictionary<string,string>>();
Dictionary<string,string> dict1 = new Dictionary<string,string>();
list.Add(dict1);
list.Add(new Dictionary<string,string>());
List<Dictionary<string,string>>
确实有效。就这样做:
var d = new List<Dictionary<string,string>>();
var toAdd = new Dictionary<string,string>();
toAdd["Test"] = "Yes";
d.Add(toAdd);
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
list.Add(new Dictionary<string, string>());
// list[0].Add(...);
尝试
List<Dictionary<string, string>> listDic = new List<Dictionary<string, string>>();
Dictionary<string, string> dic = new Dictionary<string, string>();
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic.Add("key1", "value1");
dic1.Add("key2", "value2");
listDic.Add(dic);
listDic.Add(dic1);
尝试,
List<Dictionary<int, string>> a = new List<Dictionary<int, string>>();
Dictionary<int,string> b = new Dictionary<int,string>();
b.Add(1, "Anwer");
a.Add(b);
for (int i = 0; i < a.Count; i++)
foreach (KeyValuePair<int, string> v in a[i])
Console.WriteLine(v.Key + " ------ " + v.Value);