Click here to Skip to main content
15,892,537 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.4K   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 
While HashSet perhaps deserves to be used more than it is, one reason is isn't used a lot is that it's kind of inconvenient. For example, you can't simply combine two sets to get a new set by writing (set1 | set2) - you can use UnionWith, but this modifies one of the original sets. And that wouldn't be so bad if it were easy to duplicate a set, but they make you write new HashSet<DataType>(oldSet) instead of the simpler oldSet.Clone().

I wrote a different kind of engine for storing sets, modeled after functional languages. Its implementation is called InternalSet and you can read about its implementation here. There are four standard wrappers for this class, Set<T>, MSet<T>, Map<T>, and MMap<T>.


  • Set<T> is an immutable set. You can use the set1 | set2 to merge sets, you can use set1 & set2 to compute an intersection, and similarly ^ is for XOR, - computes the difference between two sets, and + can be used to insert single items.
  • MSet<T> is a mutable set, like HashSet, but more convenient since it has all the same operators as Set.
  • Map<Key,Value> is an immutable map of key-value pairs, with lots of convenient methods for combining maps and adding items.
  • MMap<Key,Value> is a mutable map of key-value pairs, which works exactly like Dictonary<Key,Value> except that it has a few extra capabilities.


You can cast from MSet to Set or vice versa in constant time ("instantly"). You can also cast from MMap to Map or vice versa in constant time. Finally, there is an extra class InvertibleSet<T> which represents a set that may be inverted, i.e. you could have a set of numbers {2, 3, 5, 7}, or you could have an inverted set of all numbers except {2, 3, 5, 7}.

All of this is part of my Loyc Core libraries, available on NuGet. Learn more at core.loyc.net.

Update: I've written an article about all this.

modified 25-Feb-16 22:53pm.

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.