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 to store only the strongly typed elements i.e. the values of specified data type.


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


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


In c#, the hashset is not useful when we want work with duplicate elements in that case we need to consider using 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, collection is a class so to define a hashset, we must need to declare an instance of hashset class before we perform any operations such as add, delete, etc. like as shown below.


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

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


Here, the angle (<>) brackets will indicate that the hashset is a generic type and type parameter T is to represent a type of values to be accepted by 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 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

Following are the some of commonly used properties of hashset object in c# programming language.


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

C# HashSet Methods

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


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

C# HashSet Example

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


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.







            Console.WriteLine("Number of Elemen in HashSet: {0}", hset.Count);

            Console.WriteLine("*********HashSet Elements********");

            // Accessing elements from hashset.

            foreach (int item in hset)




            // 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("Contains Value '2': {0}", hset.Contains(2));

            Console.WriteLine("Contains Value '10': {0}", hset.Contains(10));





If you observe 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 we execute above c# program, we will get the result like as shown below.


C# HashSet Example Result


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

C# HashSet Overview

Following are the important points which needs to be remember about hashset in c#.


  • Hashset is a generic collection and it will available with System.Collections.Generic namespace.
  • Hashset is used to represent a set of values.
  • Hashset will allow us to store a duplicate values but it will return only unique values in result set.
  • Hashset collection is not sorted and we can access hashset values by using foreach loop.


C# Dictionary

Topics Covered