HashSet In C#

Introduction

 
C# HashSet is an unordered collection that contains unique elements. The HashSet<T> class represents a hash set that is defined in the System.Collections.Generic namespace. .NET HashSet class provides several methods to add items, remove items, find and search items. The HashSet class also offers methods to compare two objects and supports operations such as union, intersection, subset, superset, overlap and symmetric differences.
 

Create Hashset<T>

 
Like any other object in .NET and C#, we use the new operator to create a HashSet object and pass a data type. The following code snippet creates two objects of int type data. 
  1. HashSet<int> firstset = new HashSet<int>();  
  2. HashSet<int> secondset = new HashSet<int>();  

Add Items to HashSet

 
The Add method is used to insert items to a HashSet. The following code snippet adds items to both of the HashSet objects defined in the previous section. 
  1. for (int i = 1; i < 10; i++) {  
  2.     firstset.Add(5 * i);  
  3. }  
  4. for (int j = 1; j < 10; j++) {  
  5.     secondset.Add(10 * j);  
  6. }  

Read HashSet Items

 
All collection objects in C# and .NET can be read using a foreach loop. The following code snippet reads both HashSet collections and prints their itesm to the console.
  1. namespace ConsoleApplication14 {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             HashSet < int > firstset = new HashSet < int > ();  
  5.             HashSet < int > secondset = new HashSet < int > ();  
  6.             for (int i = 1; i <= 10; i++) {  
  7.                 firstset.Add(5 * i);  
  8.             }  
  9.             for (int j = 1; j <= 10; j++) {  
  10.                 secondset.Add(10 * j);  
  11.             }  
  12.             Console.WriteLine("The table of five(5) is:");  
  13.             foreach(int a in firstset) {  
  14.                 Console.WriteLine(a);  
  15.             }  
  16.             Console.WriteLine("The table of Ten(10) is:");  
  17.             foreach(int b in secondset) {  
  18.                 Console.WriteLine(b);  
  19.             }  
  20.         }  
  21.     }  
Here is the output.

HashSet In C#
 

Remove Items from HashSet

 
The Remove() method removes items from a HashSet. The Remove method takes the item as an input and removes it from the collection.
 
The following code snippet is an example of using the Remove method.
  1. namespace ConsoleApplication14 {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             HashSet < int > firstset = new HashSet < int > ();  
  5.             for (int i = 1; i <= 10; i++) {  
  6.                 firstset.Add(5 * i);  
  7.             }  
  8.             Console.WriteLine("The table of five(5) is:");  
  9.             foreach(int a in firstset) {  
  10.                 Console.WriteLine(a);  
  11.             }  
  12.             for (int i = 1; i <= 10; i++) {  
  13.                 if (i < 8) {  
  14.                     firstset.Remove(5 * i);  
  15.                 }  
  16.             }  
  17.             Console.WriteLine("After Removal the elements the hashset is as,");  
  18.             foreach(int a in firstset) {  
  19.                 Console.WriteLine(a);  
  20.             }  
  21.         }  
  22.     }  
  23. }  
Here is the output.
 
HashSet In C#
 

Finding Items in HashSet

 
The Contains method of HashSet is used to find items in a collection. This method finds an element and if the element is present in the list, it returns true, otherwise it returns false. The following code example uses Contains method to find items in a collection. 
  1. namespace ConsoleApplication14 {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             HashSet < int > firstset = new HashSet < int > ();  
  5.             for (int i = 1; i <= 10; i++) {  
  6.                 firstset.Add(5 * i);  
  7.             }  
  8.             Console.WriteLine("The table of five(5) is:");  
  9.             foreach(int a in firstset) {  
  10.                 Console.WriteLine(a);  
  11.             }  
  12.             Console.WriteLine("ITem contain is:" + firstset.Contains(15));  
  13.             Console.WriteLine("ITem contain is:" + firstset.Contains(55));  
  14.             Console.WriteLine("ITem contain is:" + firstset.Contains(45));  
  15.         }  
  16.     }  
  17. }  
Here is the output.
 
HashSet In C#
 

Summary

 
In this article, I've explained C# HashSet and how to use its methods and properties.


Similar Articles