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

Stats

7.7K views
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...

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

Share

About the Author

SubhamoyBurman
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