Mid
From PDF
Collections
C# Collections
Can a HashSet<T> store duplicate values?
Short answer: No, HashSet<T> does not allow duplicates. Attempting to add a duplicate value will return false and not change the set.
Example code
bool added = uniqueNumbers.Add(2); // returns false because 2 already exists
Real-world example (ShopNest)
When applying a coupon, ShopNest keeps used coupon codes in a HashSet<string> so “already used?” checks stay fast and unique.
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