HashSet .Net 3.5

The C# HashSet<T> data structure was introduced in the .NET Framework 3.5.
It is a very powerful data structure with no duplicate values. It comes with a bunch of built-in set functions. It inherits the ICollection interface, so the elements inside a HashSet can only be accessed through an enumerator.

Why just not use normal List<T>?

Because HashSet has a hash function inside, so it takes less time to find value in a collection. When size of collection is gets bigger then performance of searching speed is extremely faster than a normal List.

Here is sample code for HashSet

HashSet set = new HashSet();
set.Add(“aaa”); //add duplicate “aaa”
foreach(string setItem in set)


The documentation for HashSet says “the elements are in no particular order.” This means that, as with other un-ordered collection objects that the runtime does not guarantee they will be returned in any specific order. As a consequence your code should not rely on a particular ordering. Two iterations in the same execution of the same routine could return items in a vastly (or just slightly) different order.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s