Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Up and running with HashSet in C#

0.00/5 (No votes)
15 Feb 2016 1  
HashSet is one of the most useful at the same time underused features in .NET/C#. Lets explore it good detail...

Introduction

A Hashset in mathematical sense its a collection of new items which will not allow you to store duplicates inside. This behavior is useful in number of scenarios like you are keeping a list of employees in a department in that case you have to make sure no employee is added twice in that list. In this kind of scenarios a HashSet will enforce that rule. The most popular and overused generic type - List<T> doesn't help in this case. 

Who should read this?

Any C# developer who is trying to use HashSet<T> datastructure in their application.

Using the code

Lets directly jump into coding and get our hands dirty a bit. 

Create a Console Application to explore it . And lets put the below lines of code into it. 

HashSet<int> intSet = new HashSet<int>();
  intSet.Add(1);
  intSet.Add(2);
  intSet.Add(3);
  intSet.Add(2);

 foreach (var item in intSet)
 {
   Console.WriteLine(item);
 }

Console.ReadLine();

So above we are declaring a HashSet<int> object.  In that object we have added 4 integers 1,2,3,2 . So we are adding twice the 2 int. When we enumerate the set and display it we will see it is only displaying 1,2,3 two is not added for the second time. 

Mathematical Operations using Hashset

There are set based operations we can do using HashSet<T>. 

Using Intersection

var set1 = new HashSet<int>() { 1, 2, 3 };
var set2 = new HashSet<int>() { 2, 3, 4 };

            set1.IntersectWith(set2);

            foreach (var item in set1)
            {
                Console.WriteLine(item);
            }

So there are two sets we have and we are trying to find the intersection between them. Try this program and see what is the output of it. It should be { 2 , 3 }. 

Using Union

var set1 = new HashSet<int>() { 1, 2, 3 };
var set2 = new HashSet<int>() { 2, 3, 4 };

            set1.UnionWith(set2);

            foreach (var item in set1)
            {
                Console.WriteLine(item);
            }

Using UnionWith we can get two sets Union as well.  O/P of the above program should be  { 1,2,3,4 }. 

 

Comparing two Objects using HashSet<T>

When you are adding two objects with the same properties HashSet will add them without any question.

HashSet<Employee> set = new HashSet<Employee>();
            set.Add(new Employee { Name = "Subha" });
            set.Add(new Employee() { Name = "Subha" });

            foreach (var item in set)
            {
                Console.WriteLine(item.Name);
            }

In the above example HashSet<T> doesn't have any information whether the Employee objects are same or not. So it will add both of them as they are separate object reference.

But definitely the same object reference cannot be added into HashSet<T>. 

HashSet<Employee> set = new HashSet<Employee>();
            var employee = new Employee() { Name = "Subham"};
            set.Add(employee);
            set.Add(employee);

            foreach (var item in set)
            {
                Console.WriteLine(item.Name);
            }

In the above case as we are trying to add same object reference. HashSet<T> won't allow you to do so. Rather it will add only one object.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here