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.
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.
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.
Following is the example of initializing a generic hashset by specifying a required type to accept values.
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.
The following are some of the commonly used properties of hashset objects in the c# programming language.
Property | Description |
---|---|
Count | It is used to get the number of elements in HashSet. |
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.
Method | Description |
---|---|
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. |
Following is the example of adding and accessing elements from hashset in c# programming language.
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.
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.
The following are the important points that need to remember about HashSet in c#.