Click here to Skip to main content
15,886,069 members
Articles / Programming Languages / C# 3.5

Up and running with HashSet in C#

Rate me:
Please Sign up or sign in to vote.
3.86/5 (17 votes)
15 Feb 2016CPOL2 min read 19.3K   12   7
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. 

C++
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. 

Image 1

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
.NET developer with close proximity to Mobile Technology(Xamarin/Windows Phone). Also passionate about ASP.NET MVC/WebApi development

Comments and Discussions

 
QuestionHashList Pin
  Forogar  4-Mar-16 11:24
professional  Forogar  4-Mar-16 11:24 
GeneralMy vote of 5 Pin
CeGu25-Feb-16 21:33
professionalCeGu25-Feb-16 21:33 
QuestionMore convenient alternative to HashSet Pin
Qwertie25-Feb-16 14:19
Qwertie25-Feb-16 14:19 
QuestionNice article about HashSet. Pin
Kannan Paramasivam17-Feb-16 11:25
Kannan Paramasivam17-Feb-16 11:25 
PraiseGood work. Pin
edaniel198416-Feb-16 8:32
edaniel198416-Feb-16 8:32 
QuestionMy vote of 4 Pin
Jens Madsen, Højby16-Feb-16 4:59
Jens Madsen, Højby16-Feb-16 4:59 
GeneralMy vote of 5 Pin
Member 1192174015-Feb-16 19:44
Member 1192174015-Feb-16 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.