How can you remove all items from a Dictionary? Follow:
Short answer: Use the Clear() method to remove all key-value pairs.
Explain a bit more
dictionary.Clear(); This resets the dictionary to an empty state. π C# Queue<T> β Interview Questions & Use the Clear() method to remove all key-value pairs. dictionary.Clear(); This resets the dictionary to an empty state. π C# Queue<T> β Interview Questions & Use the Clear() method to remove all key-value pairs. dictionary.Clear(); This resets the dictionary to an empty state. π C# Queue<T> β Interview Questions & Use the Clear() method to remove all key-value pairs. dictionary.Clear(); This resets the dictionary to an empty state. π C# Queue<T> β Interview Questions &
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