Updated March 28, 2023
Introduction to C# HashSet
A unique collection of elements and not in order is called a HashSet<T> in C#, which comes under the namespace Systems.Collections.Generic and is used whenever we do not need any duplicates in our collection, i.e., its avoids insertion of duplicates into the collection and to compare the performance of HashSet, HashSet provides better performance compared to list and set operations provided by the HashSet gives high performance and the number of elements held by the object is the capacity of the HashSet which increases with the increase in the number of elements.
Syntax of HashSet in C#
HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();
The above syntax represents HashSet in C#. The type of hashset can also be represented by the capital letter T.
Working of Hashset in C#
Hashset in C# is a unique collection of elements without any order. It comes under the namespace Systems.Collections.Generic and is used whenever we do not need any duplicates in our collection, i.e., it avoids insertion of duplicates into the collection and compares the performance of HashSet. HashSet provides better performance compared to list set operations provided by the HashSet gives high performance. In order to understand the working of hashset, let’s begin by creating a simple hashset program in C#.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
In the above program, a simple hashset of type strings is created. The strings Shobha, Shivakumar, and Shardha are added to the created hashset of type strings. The output of the above program is shown in the snapshot below:
As already explained, the hashset does not allow duplicate elements to be added in the collection. Let us try adding a duplicate element to the above-created hashset of type strings and understand the output of the program. Then, we can use add a method to add the elements into the collection.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
nameslist.Add("Shobha");
//collection cannot contain duplicate elements.
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
In the above program, a simple hashset of type strings is created. Then, the strings Shobha, Shivakumar, and Shardha are added to the created hashset of type strings. If we try to add a duplicate string Shobha to the created hashset using add method, no error is shown, but it does not add the duplicate string, and the same can be seen in the output. The output of the above program is shown in the snapshot below:
There is a method called Union with method present in hashset in C#. It is used to combine the elements present in two collections into a single collection on which it is called. Consider the below program.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.UnionWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Then, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we make use of union with the method is hashset to combine the elements of both first hashset and second hashset. The output of the above program is shown in the snapshot below:
There is a method called Intersect with the method present in hashset in C#. It is used to combine the elements present in common in the two collections into a single collection on which it is called. Consider the below program.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.IntersectWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Next, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we use intersecting with the method is hashset to combine the common elements in both the first hashset and second hashset. The output of the above program is shown in the snapshot below:
There is a method called Except with method present in hashset in C#. It is used to remove all the elements present in all the two collections except the collection on which it is called. Consider the below program.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist1.ExceptWith(nameslist);
foreach(var nam in nameslist1) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Next, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we make use of Except with the method in hashset to remove all the elements present in all the collections except the collection on which it is called. The output of the above program is shown in the snapshot below:
Examples of C# HashSet
Given below are the examples of C# HashSet:
Example #1
C# program to illustrate the creation of a hashset and adding elements to the hashset.
Code:
using System;
using System.Collections.Generic;
class Hashset {
// Calling the main method
public static void Main()
{
// An hashset of even numbers is created
HashSet<int> even = new HashSet<int>();
// Adding the elements in to the hashset
for (int i = 0; i < 5; i++) {
even.Add(2 * i);
}
// Printing the elements in the hashset
foreach(int j in even)
{
Console.WriteLine(j);
}
}
}
Output:
Example #2
C# program to illustrate contains method in a hashset.
Code:
using System;
using System.Collections.Generic;
class hashset {
// Calling the main method
public static void Main()
{
// An hashset consisting of strings is created
HashSet<string> cl = new HashSet<string>();
// Elements are inserted into the hashset
cl.Add("Shobha");
cl.Add("Shivakumar");
cl.Add("Shardha");
cl.Add("Ravi");
// Using the contains method to check if an element is present in the collection
if (cl.Contains("Shobha"))
Console.WriteLine("The element specified is present");
else
Console.WriteLine("The element specified is not present");
}
}
Output:
Recommended Articles
This is a guide to C# HashSet. Here we discuss the concept of Hashset in C# through definition and then understand the syntax and working of Hashset in C# through example programs. You may also have a look at the following articles to learn more –