Mid
From PDF
Collections
C# Collections
How do you get all keys and values from a Dictionary<TKey, TValue>?
Short answer: dictionary.Keys – returns a collection of all keys dictionary.Values – returns a collection of all values Example: foreach (var key in dictionary.Keys) Console.WriteLine(key); foreach (var value in dictionary.Values) Console.WriteLine(value);
Example code
dictionary.Keys – returns a collection of all keys dictionary.Values – returns a collection of all values Example: foreach (var key in dictionary.Keys) Console.WriteLine(key); foreach (var value in dictionary.Values) Console.WriteLine(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
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png