Udemy logo

c sharp hashsetThe .NET framework provides specific classes to store a collection of items. These collections are of two types: Non-generic collections which belong to System.Collections namespace and Generic collections which belong to System.Collections.Generic namespace. HashSet is a type of generic collections and is denoted as HashSet<T> like all other generic collections. In this article, both HashSet and HashSet<T> are the terms used for generic HashSet collections.

How C# HashSet is different from other collections?

The following are the three basic characteristics that differentiate C# HashSet<T> from other generic and non-generic collections in the .NET framework:

HashSet is implemented via a Hashtable and implements ICollection<T> interface.

To learn more about C# collections, take a course at Udemy

A Simple C# HashSet Example

The following example shows how a simple C# HashSet can be implemented and how Contains method work:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE" };
Console.WriteLine(countries.Contains("AUSTRALIA"));
Console.WriteLine(countries.Contains("ENGLAND"));
Console.ReadLine();

In the above example, a HashSet collection of type string, named countries has been defined via a HashSet constructor where values have been passed dynamically during definition. In the next lines of code, the Contains method has been used to find if the countries collection contains AUSTRALIA and ENGLAND respectively. The first call to Contains method would return true since AUSTRALIA is present in the countries HashSet collection whereas the second call to Contains would return false since ENGLAND is not present in the collection.

For more interesting C# tutorials, check out this class at Udemy

Adding Elements to a C# HashSet

In order to add elements to C# HashSet, the Add method is used which takes an object of type of the HashSet collection. The following example explains this concept:

countries.Add("ITALY");
countries.Add("MEXICO");
countries.Add("ITALY");

One thing to note here is that when ITALY has been added twice in the countries collection in the above code, but when the countries collection is enumerated, only one country with name ITALY would be found. This is due to the fact that when second ITALY string is added, HashSet checks whether the new added object is already present in the collection. If it isn’t already present, it is added to the collection, or the newly added element is ignored. This results in a unique collection of elements in the HashSet.

Traversing and Enumerating a C# HashSet

C# HashSet implements both IEnumerable<T> and IEnumerable interface. The simplest way to enumerate any collection which implements one of these two interfaces is to use the foreach loop. The following example extends the example in the last section and shows how the countries collection can be enumerated via a foreach loop:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE" };
foreach (string coun in countries)
{
Console.WriteLine(coun);
}
Console.ReadLine();

You can see in the above example how simple it is to enumerate through each element of the HashSet collection and to display its value.

Removing Elements from a C# HashSet

To remove elements of a HashSet, the Remove method is used. The Remove method takes an object to remove as a parameter. The following example explains how the Remove method works:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE" };
countries.Remove("AUSTRALIA");
foreach (string coun in countries)
{
Console.WriteLine(coun);
}

The HashSet collection ‘countries’ contains some strings representing names of some random countries. Remove method has been used to remove the element AUSTRALIA from the countries collection. If the countries collection is enumerated after the Remove method, the output would not contain AUSTRALIA.

Another extremely useful method to remove elements from a HashSet is the RemoveWhere method which takes a lambda expression or a predicate. It removes the elements from the collection based on the condition specified by the predicate. The following example explains this concept:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE" };
countries.RemoveWhere(c => c.Contains("US"));
foreach (string coun in countries)
{
Console.WriteLine(coun);
}

In the above lines of code, the RemoveWhere method has been used to remove all those elements from the countries collection which contain a substring “US”. You can see from the list that, USA, AUSTRALIA and RUSSIA are the countries which contain US in its names. Therefore, if the countries collection is enumerated after RemoveWhere method in the above case, these three elements will not be displayed.

Interested in learning more about HashSet? Take this course

UnionWith method in C# HashSet

UnionWith performs a union of the elements of two HashSets and stores the value of the union in the method which calls the UninoWith. The following example demonstrates this concept:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA"};
HashSet<string> countries2 = new HashSet<string>{"CANADA", "CHINA", "RUSSIA", "FRANCE"};
countries.UnionWith(countries2);

 

There are two HashSet collections namely countries and countries2.  The UnionWith method has been called on the countries collection and countries2 collection has been passed to it. If the countries collection is enumerated, it will contain union of elements of countries collection and countries2 collection.

IntersectWith method in C# HashSet

IntersectWith method returns the intersection of the elements of the HashSet on which IntersectWith is called and the collection that has been passed to it. The following example shows how IntersectWith method actually works:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE"};
HashSet<string> countries2 = new HashSet<string>{"CANADA", "CHINA", "RUSSIA", "FRANCE"};
countries.IntersectWith(countries2);

 

The countries collection would contain CANADA, CHINA, RUSSIA and FRANCE since these are the elements that intersect in both collections.

ExceptWith method in C# HashSet

The ExceptWith method returns all the elements of the collection on which ExceptWith method has been called except the elements that are common in the both collections. The ExceptWith method is explained with the help of following example:

HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE"};
HashSet<string> countries2 = new HashSet<string>{"CANADA", "CHINA", "RUSSIA", "FRANCE"};
countries.ExceptWith(countries2);

The countries collection would now contain JAPAN, USA & AUSTRALIA only, since the last four elements of the countries collection are also present in the countries2 collection.

For more exciting C# tutorials, take this course

Page Last Updated: April 2014

Top courses in C# (programming language)

.NET / C# Interview Questions with Answers.
Shivprasad Koirala
4.7 (2,028)
Design Patterns in C# Made Simple
Zoran Horvat
4.6 (709)
The Unity C# Survival Guide
Jonathan Weinberger, Unity Technologies, GameDevHQ Team
4.8 (1,834)
How To Write Bulletproof Multi-Threaded C# Code
Mark Farragher
4.8 (1,319)
Highest Rated
Complete C# Unity Game Developer 3D
Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (42,824)
Bestseller
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (31,322)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.5 (12,413)
Bestseller
Ultimate C# Masterclass for 2024
Krystyna Ślusarczyk
4.7 (4,221)
Bestseller

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo