How do I create a List of Dictionaries in .NET?

前端 未结 4 2050
清酒与你
清酒与你 2021-02-04 08:42

I am trying to create a List of Dictionary items. I am not sure how to add items in list and how to get back the values while traversing the list.

4条回答
  •  -上瘾入骨i
    2021-02-04 09:21

    I think this is what you are looking for?

    {
        MyList.Add(new Dictionary());
        MyList.Add(new Dictionary());
        MyList[0].Add("Dictionary 1", 1);
        MyList[0].Add("Dictionary 1", 2);
        MyList[0].Add("Dictionary 2", 3);
        MyList[0].Add("Dictionary 2", 4);
        foreach (var dictionary in MyList)
            foreach (var keyValue in dictionary)
                Console.WriteLine(string.Format("{0} {1}", keyValue.Key, keyValue.Value));
    }
    

提交回复
热议问题