Click here to Skip to main content
12,768,052 members (44,689 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 14 Feb 2016

Up and running with HashSet in C#

, 15 Feb 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
HashSet is one of the most useful at the same time underused features in .NET/C#. Lets explore it good detail...


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>();

 foreach (var item in intSet)


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 };


            foreach (var item in set1)

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 };


            foreach (var item in set1)

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)

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"};

            foreach (var item in set)

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

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

You may also be interested in...

Comments and Discussions

QuestionHashList Pin
Forogar 4-Mar-16 12:24
member Forogar 4-Mar-16 12:24 
GeneralMy vote of 5 Pin
CeGu25-Feb-16 22:33
memberCeGu25-Feb-16 22:33 
QuestionMore convenient alternative to HashSet Pin
Qwertie25-Feb-16 15:19
memberQwertie25-Feb-16 15:19 
QuestionNice article about HashSet. Pin
Kannan Paramasivam17-Feb-16 12:25
memberKannan Paramasivam17-Feb-16 12:25 
PraiseGood work. Pin
edaniel198416-Feb-16 9:32
memberedaniel198416-Feb-16 9:32 
QuestionMy vote of 4 Pin
Jens-Madsen16-Feb-16 5:59
memberJens-Madsen16-Feb-16 5:59 
GeneralMy vote of 5 Pin
Member 1192174015-Feb-16 20:44
memberMember 1192174015-Feb-16 20: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 15 Feb 2016
Article Copyright 2016 by SubhamoyBurman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid