C# HashSet (HashSet<T>)

In c#, HashSet is a generic type of collection, and it is used to represent a set of unique values. The hashset in c# will allow storing only the strongly typed elements, i.e., the values of the specified data type.

 

In c#, the hashset will provide high-performance set operations, and the set will return unique elements, and those elements will not be in a particular order. Even if we add duplicate elements in hashset, it will return only the unique elements in the result set.

 

The size of hashset object will vary dynamically so we can add or remove elements from the hashset based on our requirements.

 

In c#, the hashset is not useful when you want to work with duplicate elements. In that case, you need to consider using the list object in our application.

C# HashSet Declaration

In c#, hashset is a generic type of collection, and it is provided by System.Collections.Generic namespace.

 

As discussed, the collection is a class, so to define a hashset, you need to declare an instance of hashset class before performing any operations such as add, delete, etc., like as shown below.

 

HashSet<T> hset = new HashSet<T>();

If you observe the above hashset declaration, we created a generic hashset (hset) with an instance of hashset class using type parameter (T) as placeholders with angle (<>) brackets.

 

Here, the angle (<>) brackets will indicate that the hashset is a generic type and type parameter T represents a type of values accepted by the hashset.

C# HashSet Initialization

Following is the example of initializing a generic hashset by specifying a required type to accept values.

 

HashSet<int> hset = new HashSet<int>();

If you observe the above example, we defined a hashset (hset) with a required value type to store. Here, the hashset object will store a value of int type.

C# HashSet Properties

The following are some of the commonly used properties of hashset objects in the c# programming language.

 

PropertyDescription
Count It is used to get the number of elements in HashSet.

C# HashSet Methods

The following are some of the commonly used methods of generic hashset to perform add, search, insert, delete or sort operations in the c# programming language.

 

MethodDescription
Add It is used to add elements of the specified type to hashset.
Clear It will remove all the elements from hashset.
Contains It is used to determine whether the specified element exists in hashset or not.
CopyTo It is used to copy the elements of a hashset object to an array.
Remove It is used to remove the specified element from hashset.
TryGetValue It is used to search for the given value and return if value finds.

C# HashSet Example

Following is the example of adding and accessing elements from hashset in c# programming language.

 

using System;
using System.Collections.Generic;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          //Create a new hashset
          HashSet<int> hset = new HashSet<int>();
          // Add elements to hashset object.
          hset.Add(1);
          hset.Add(2);
          hset.Add(2);
          hset.Add(3);
          hset.Add(3);
          hset.Add(4);
          Console.WriteLine("Number of Elemen in HashSet: {0}", hset.Count);
          Console.WriteLine("*********HashSet Elements********");
          // Accessing elements from hashset.
          foreach (int item in hset)
          {
             Console.WriteLine(item);
          }
          // Creating and initializing hashset
          HashSet<string> hset2 = new HashSet<string> { "welcome", "to", "tutlane", "tutlane" };
          Console.WriteLine("Number of Elemen in HashSet: {0}", hset2.Count);
          Console.WriteLine("*********HashSet2 Elements********");
          // Accessing elements.
          foreach (string item in hset2)
          {
             Console.WriteLine(item);
          }
          Console.WriteLine("Contains Value '2': {0}", hset.Contains(2));
          Console.WriteLine("Contains Value '10': {0}", hset.Contains(10));
          Console.ReadLine();
       }
    }
}

If you observe the above example, we are able to define a new generic hashset (hset, hset2) collections by using System.Collections.Generic namespace. Here, we added a defined data type values to the newly created hashsets (hset, hset2) in different ways and used a foreach loop to access elements from hashset.

 

When you execute the above c# program, you will get the result as shown below.

 

C# HashSet Example Result

 

If you observe the above result, we got only the unique elements from the hashset, and it returns the count of unique elements available in the hashset.

C# HashSet Overview

The following are the important points that need to remember about HashSet in c#.

 

  • HashSet is a generic collection, and it will be available with System.Collections.Generic namespace.
  • HashSet is useful to represent a set of values.
  • HashSet will allow us to store duplicate values, but it will return unique values in the result set.
  • HashSet collection is not sorted, and you can access HashSet values by using a foreach loop.