C# HashSet is an unordered collection of unique elements. It was introduced in .NET 3.5 and is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. In this article, I will begin with creating a simple HashSet, and continue to perform the various operations on HashSet. At the end of the article, I will show how to create a HashSet of custom type and how to prevent the duplicates from being inserted in HashSet.
Let's begin our example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSetDemo {
class Program {
static void Main(string[] args) {
HashSet < string > names = new HashSet < string > {
"Rajeev",
"Akash",
"Amit"
};
foreach(var name in names) {
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}

In the code, given above, we are creating a simple HashSet of the string type and adding the strings to it. We can also add the string, using the Add method. We will see how we can use the Add method in the snippet, given below. We will now try to add the duplicate string and see what happens.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSetDemo {
class Program {
static void Main(string[] args) {
HashSet < string > names = new HashSet < string > {
"Rajeev",
"Akash",
"Amit"
};
names.Add("Rajeev");
//duplicates are not added into collection.
foreach(var name in names) {
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}

In the snippet, given above, even though we try to add a duplicate string, we will not get any error but when we iterate the collection, we cannot find the string. This shows that we cannot add the duplicate elements to a HashSet. Now, we will look into some of the important methods of HashSet.
UnionWith
This method combines the elements, present in both the collections into the collection on which it is called.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSetDemo {
class Program {
static void Main(string[] args) {
HashSet < string > names = new HashSet < string > {
"Rajeev",
"Akash",
"Amit"
};
HashSet < string > names1 = new HashSet < string > {
"Rajeev",
"Akash",
"Amit",
"Deepak",
"Mohit"
};
names.UnionWith(names1);
foreach(var name in names) {
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}

IntersectWith
This method combines the elements that are common to both the collections.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSetDemo {
class Program {
static void Main(string[] args) {
HashSet < string > names = new HashSet < string > {
"Rajeev",
"Akash",
"Amit"
};
HashSet < string > names1 = new HashSet < string > {
"Rajeev",
"Akash",
"Amit",
"Deepak",
"Mohit"
};
names.IntersectWith(names1);
foreach(var name in names) {
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}




Jaydeep PatilPosted Jul 20, 2022, 3:40 PM
Nice Article Rajeev Keep Posting
Sunil jadhavPosted Oct 3, 2019, 4:06 AM
Nice article. Very useful one.
Pankajkumar PatelPosted Sep 27, 2019, 12:26 AM
Nice article
Maytas TaytasPosted Jul 23, 2019, 4:52 PM
Thanks, Rajeev. A want to as k a question. IEquatable intarface include Equals method, but not include GetHashCode method. Where did this method(GetHashCode) come from?
Raja TPosted Jul 7, 2016, 10:37 AM
Nice, Thanks for sharing
kalu singh raoPosted Jul 7, 2016, 1:28 AM
Nice...
RajaPosted Jul 7, 2016, 12:12 AM
Nice Share...
Praveen SreeramPosted Jul 6, 2016, 11:01 PM
Very nice