Finally, a Set data structure was added to C# in the .NET Framework 3.5. It’s a powerful data structure that makes life a little easier for programmers…

The C# set is called a HashSet. It is based on a mathematical set, which means that the elements must be unique. In other words, a Set guarantees that there are no duplicate elements.

Now why create a Set data structure in C# when one could simply check a list before adding an element to see if it already exists? The answer: it’s because searching an ordinary list is slow. A HashSet is fundamentally designed to allow fast lookups, making inserts faster.

There are different implementations of Sets. Some make insert and lookup operations super fast by hashing elements. However, that means that the order in which the elements were added is lost. Other implementations preserve the aggregate order at the cost of slower execution times.

The HashSet class in C# takes the second approach, thus preserving the order of the elements. It’s still much faster than a normal list. Some basic benchmarks showed that HashSet is decently faster when dealing with primary types (int, double, bool, etc.). It is much faster when dealing with working class objects. So that point is that HashSet is fast.

The only drawback of HashSet is that there is no index access. To access the elements you can either use an enumerator or use the built in function to convert the HashSet to a List and iterate through that.

Leave a Reply

Your email address will not be published. Required fields are marked *