The answer depends a little on what you are trying to achieve. There may even be a data structure that would be easier to use. But lets say you want to add loweststringa
and loweststringb
for innerstring1
for outerstring
(ooohh that is complex).
Then this would do it:
List<string> lowestList = new List<string>();
lowestList.AddRange(new[]{"loweststringa", "loweststringb"});
Dictionary<string, List<string>> innerDictionary = new Dictionary<string, List<string>>();
innerDictionary.Add("innerstring1", lowestList);
Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
myDictionary.Add("outerstring", innerDictionary);
The keys you add to myDictionary
are of type string
. The values of myDictionary
are instances of a Dictionary<string, List<string>>
.
The keys of these inner dictionaries are of course also of type string
and the values are of type List<string>
.
If you want to check if "loweststringb" exists in the list for "innerstring1" in the dictionary for "outerstring" you can do the following:
if (myDictionary.ContainsKey("outerstring"))
if (myDictionary["outerstring"].ContainsKey("innerstring1"))
if (myDictionary["outerstring"]["innerstring1"].Contains("loweststringb"))
// hooray it's there!
But be sure you don't insert null
references as Dictionaries or Lists in this construction. And the example above is not the most efficient since it iterates through the outermost dictionary three times.
Hope this helps you.
UPDATE: inserting "loweststringc" in the existing list:
myDictionary["outerstring"]["innerstring1"].Add("loweststringc");
>`
– Sam I am says Reinstate Monica Dec 07 '15 at 17:02