如何存储词典集合?

时间:2011-05-11 10:18:32

标签: c# dictionary

我需要在C#中创建不同数量的Dictionary<string, string>;我怎么能这样做?

我已尝试使用List<Dictionary<string, string>>,但这不起作用。

5 个答案:

答案 0 :(得分:1)

见这个例子:

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>());

答案 1 :(得分:1)

List<Dictionary<string,string>>确实有用。就这样做:

var d = new List<Dictionary<string,string>>();
var toAdd = new Dictionary<string,string>();
toAdd["Test"] = "Yes";

d.Add(toAdd);

答案 2 :(得分:0)

List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
list.Add(new Dictionary<string, string>());
// list[0].Add(...);

答案 3 :(得分:0)

尝试

  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);

答案 4 :(得分:0)

尝试,

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);