Dofactory.com
Dofactory.com

How to iterate over a dictionary in C#

Below are 3 ways to iterate over the keys and values in a dictionary:


// iterate over C# Dictionary Keys and Values

foreach (KeyValuePair<string, string> entry in myDictionary)
{
   // do something with entry.Value or entry.Key
}

// itrate over C# Dictionary Values only

foreach (var key in myDictionary.Keys)
{
   // do something with key
}

// iterate over C# Dictionary Keys only

foreach (var value in myDictionary.Values)
{
   // do something with value
}


Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.