HashSet in C# with Examples
In C#, HashSet is an unordered collection of unique elements. This collection is introduced in .NET 3.5. It supports the implementation of sets and uses the hash table for storage. This collection is of the generic type collection and it is defined under System.Collections.Generic namespace. It is generally used when we want to prevent duplicate elements from being placed in the collection. The performance of the HashSet is much better in comparison to the list.
- The HashSet class implements the ICollection, IEnumerable, IReadOnlyCollection, ISet, IEnumerable, IDeserializationCallback, and ISerializable interfaces.
- In HashSet, the order of the element is not defined. You cannot sort the elements of HashSet.
- In HashSet, the elements must be unique.
- In HashSet, duplicate elements are not allowed.
- Is provides many mathematical set operations, such as intersection, union, and difference.
- The capacity of a HashSet is the number of elements it can hold.
- A HashSet is a dynamic collection means the size of the HashSet is automatically increased when the new elements are added.
- In HashSet, you can only store the same type of elements.
How to create a HashSet?
The HashSet class provides 7 different types of constructors which are used to create a HashSet, here we only use HashSet(), constructor. To read more about HashSet’s constructors you can refer to C# | HashSet Class.
HashSet(): It is used to create an instance of the HashSet class that is empty and uses the default equality comparer for the set type.
Step 1: Include System.Collections.Generic namespace in your program with the help of using keyword:
Step 2: Create a HashSet using the HashSet class as shown below:
HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();
Step 3: If you want to add elements in your HashSet, then use Add() method to add elements in your HashSet. And you can also store elements in your HashSet using collection initializer.
Step 4: The elements of HashSet is accessed by using a foreach loop. As shown in the below example.
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000
How to remove elements from the HashSet?
In HashSet, you are allowed to remove elements from the HashSet. HashSet<T> class provides three different methods to remove elements and the methods are:
- Remove(T): This method is used to remove the specified element from a HashSet object.
- RemoveWhere(Predicate): This method is used to remove all elements that match the conditions defined by the specified predicate from a HashSet collection.
- Clear: This method is used to remove all elements from a HashSet object.
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0
HashSet class also provides some methods that are used to perform different operations on sets and the methods are:
- UnionWith(IEnumerable): This method is used to modify the current HashSet object to contain all elements that are present in itself, the specified collection, or both.
C C++ C# Java Ruby PHP Perl
- IntersectWith(IEnumerable): This method is used to modify the current HashSet object to contain only elements that are present in that object and in the specified collection.
- ExceptWith(IEnumerable): This method is used to remove all elements in the specified collection from the current HashSet object.
C C# Ruby