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.
I think you have to know in which of the dictinaries you have to add your new value. So The List is the problem. You can't identify the dictionary inside.
My solution for this would be a dictionary collection class. It could look like this:
public class DictionaryCollection : Dictionary> {
public void Add(string dictionaryKey,string key, TType value) {
if(!ContainsKey(dictionaryKey))
Add(dictionaryKey,new Dictionary());
this[dictionaryKey].Add(key,value);
}
public TType Get(string dictionaryKey,string key) {
return this[dictionaryKey][key];
}
}
then you can use it like this:
var dictionaryCollection = new DictionaryCollection
{
{"dic1", "Key1", 1},
{"dic1", "Key2", 2},
{"dic1", "Key3", 3},
{"dic2", "Key1", 1}
};