开发者

How to store a collection of Dictionaries?

I need to create a various number of Dictionary<string, string>s in C#; how can I do so?

I've tried u开发者_运维百科sing List<Dictionary<string, string>>, but that doesn't work.


See this example:

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>> does work. Just do it like this:

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


Try

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


Try,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜