-2

I am new to C# and have this IDictionary:

IDictionary<string, IList<string>> myVariable;

I tried this:

foreach(IList<Strintg> str in myVariable.Values){
    foreach(String subStr in str){
        Console.WriteLine(subStsr);
    }
}

This gives me the values well enough, but I want something that will give me the keys and values:

key01
    value01
    value02
    value03
    value04
    value05
    ...


key02
    value01
    value02
    value03
    value04
    value05
    ...


key03
    value01
    value02
    value03
    value04
    value05
    ...

...

How do I go about grabbing each String key for each IDictionary entry?

Brian
  • 1,726
  • 2
  • 24
  • 62

1 Answers1

0

Dictionaries can be looped with KeyValuePair<TKey,TValue> or just use var for convenience.

foreach(var pair in myVariable)
{
  string key = pair.Key;
  List<string> val = pair.Value;
  // do your thing
}

Alternatively you could also iterate over the keys and use them to access the dictionary:

foreach(string key in myVariable.Keys)
{
  List<string> val = myVariable[key];
  // do your thing
}
Jack T. Spades
  • 986
  • 6
  • 9