What is the best way to iterate over a dictionary?

Explanation

If you want to iterate over a dictionary you can use the following statements:

foreach(var item in myDictionary)
{
  foo(item.Key);
  bar(item.Value);
}

  Or in case if you just want to iterate over the collection of keys, at that time you can use:

foreach(var item in myDictionary.Keys)
{
  foo(item);
}

  And if you just want to take the values, So use the following:

foreach(var item in myDictionary.Values)
{
  foo(item);
}

 

Also read, Sort array of objects by string property value

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *