Introduction
Collections play a very important role in any programming language, so in this article, we are going to discuss the HashSet collection Introduced in .Net 3.5. This article can be used by beginners, intermediate, and professionals.
We are going to cover,
- What is HashSet?
- How to Create HashSet?
- How to Add a new element in HashSet?
- How to Handle duplicate Element in HashSet?
- How to Search element from HashSet?
- How to Remove element from HashSet?
- HashSet - Set Operational Methods
-
- IsProperSubsetOf
- UnionWith
- IntersectWith
- ExceptWith
What is HashSet?
Before we start with the definition, the first question that comes to mind is, where should we use or in which situation we should use HashSet Collection?
Suppose we have a requirement to store unique values and prevent duplicate insertion in the collection, then HashSet would be the right choice.
The next question would be, what is HashSet?
“HashSet is Unorder Collection which contains unique values to get High performance in C#.net.”
Below are a few important points of HashSet
- It can store only values not keys like other collections in C#.net.
- HashSet is unorder collection.
- Contain unique values.
- It can allow a single null value.
- It’s part of the System.Collections.Generic namespace.
- Give you high performance as it has unique values.
So we got the basic idea of HashSet. Now we will begin with creating of simple HashSet and adding a few values to it.
How to Create HashSet?
Here I am going to create a simple HashSet of string type which contains Cites.
Please see the below code,
using System;
using System.Collections.Generic;
namespace HashSetDemo
{
class Program
{
static void Main(string[] args)
{
HashSet<string> cities = new HashSet<string>
{
"Mumbai",
"Vadodra",
"Surat",
"Ahmedabad"
};
foreach (var city in cities)
{
Console.WriteLine(city);
}
Console.ReadLine();
}
}
}
In the above code,
- We have created a .Net Console application.
- Created Simple HashSet “Cities”.
- Iterate Cities and Print City on the screen.
Output

How to Add new Elements in the HashSet?
We are going to add a few elements in HashSet using Add method.
Let's see the below code,
using System;
using System.Collections.Generic;
namespace HashSetDemo
{
class Program
{
static void Main(string[] args)
{
HashSet<string> cities = new HashSet<string>
{
"Mumbai",
"Vadodra",
"Surat",
"Ahmedabad"
};
cities.Add("Bharuch");
foreach (var city in cities)
{
Console.WriteLine(city);
}
Console.ReadLine();
}
}
}
In the above code, we have used the “Add” method to insert a new element in HashSet Collection.
Output

How to Handle duplicate Element in HashSet?
A few more questions came to mind while I was adding elements in the HashSet in the previous section,
- What will happen if we try to add a duplicate element in HashSet?
- Will it throw a runtime exception?
- Will it ignore duplicate elements without throwing any exception?
Let’s see the below Code snippet to get answers to these questions. I am going to add a few duplicate cities and execute the program,
using System;
using System.Collections.Generic;
namespace HashSetDemo
{
class Program
{
static void Main(string[] args)
{
HashSet<string> cities = new HashSet<string>
{
"Mumbai",
"Vadodara",
"Surat",
"Ahmedabad",
"Bharuch"
};
cities.Add("Mumbai");
cities.Add("Vadodara");
cities.Add("Surat");
cities.Add("Ahmedabad");
cities.Add("Bharuch");
foreach (var city in cities)
{
Console.WriteLine(city);
}
Console.ReadLine();
}
}
}
In the above code,
- Added 5 cities in the HashSet
- Duplicate 5 cities added again in the HashSet with the Add method.
- Execute Program.
Output

Based on the above output, we can conclude that duplicate records are ignored without throwing any error. In the above case, only 5 cities appeared as output and ignored duplicate 5 cities.
How to Search element from HashSet?
Contain method can use to search elements from HashSet. Let’s see the below code,
using System;
using System.Collections.Generic;
namespace HashSetDemo
{
class Program
{
static void Main(string[] args)
{
HashSet<string> cities = new HashSet<string>
{
"Mumbai",
"Vadodara",
"Surat",
"Ahmedabad",
"Bharuch"
};
if(cities.Contains("Surat"))
{
Console.WriteLine("Surat found in the collection");
}
else
{
Console.WriteLine("Surat Not found in the collection");
}
Console.ReadLine();
}
}
}







Join the conversation! Your thoughts help the community grow.