How to use a foreach loop to iterate over a dictionary in C#? - Biz Tech

How to use a foreach loop to iterate over a dictionary in C#?

Listen
Dictionary<string, int> myDictionary = new Dictionary<string, int>
{
    { "apple", 1 },
    { "banana", 2 },
    { "cherry", 3 }
};

foreach (KeyValuePair<string, int> item in myDictionary)
{
    Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}