Mid From PDF Collections C# Collections

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

Short answer: Use a foreach loop with KeyValuePair<TKey, TValue>: foreach (KeyValuePair<string, int> pair in dictionary)

Example code

{ Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}"); } Or use deconstruction (C# 7+): foreach (var (key, value) in dictionary)
{
Console.WriteLine($"{key} = {value}");
}

Real-world example (ShopNest)

ShopNest caches product prices in a Dictionary<string, decimal> keyed by SKU so checkout can look up a price in O(1) instead of scanning a list.

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
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