Junior Collections

What is a HashSet<T> in C# and when would you use it?

HashSet<T> is a generic collection that stores unique elements with no particular order.

It is optimized for fast lookups, additions, and deletions.

Follow:

Use case:

When you want to store a collection of unique items without duplicates, such as user IDs,

tags, or keywords.

HashSet<int> uniqueNumbers = new HashSet<int> { 1, 2, 3 };

uniqueNumbers.Add(4);

More from C# Programming Tutorial

All questions for this course