Mid From PDF Collections C# Collections

How do you iterate over a Dictionary<TKey, TValue>?

Use a foreach loop with KeyValuePair<TKey, TValue>:

foreach (KeyValuePair<string, int> pair in dictionary)
{

Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");

}

Or use deconstruction (C# 7+):

foreach (var (key, value) in dictionary)
{
Console.WriteLine($"{key} = {value}");
}

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details