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:
using System.Collections.Generic;
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.
Example:
C#
// C# program to illustrate how to // create hashset using System; using System.Collections.Generic; class GFG { // Main Method static public void Main() { // Creating HashSet // Using HashSet class HashSet< string > myhash1 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash1.Add( "C" ); myhash1.Add( "C++" ); myhash1.Add( "C#" ); myhash1.Add( "Java" ); myhash1.Add( "Ruby" ); Console.WriteLine( "Elements of myhash1:" ); // Accessing elements of HashSet // Using foreach loop foreach ( var val in myhash1) { Console.WriteLine(val); } // Creating another HashSet // using collection initializer // to initialize HashSet HashSet< int > myhash2 = new HashSet< int >() {10, 100,1000,10000,100000}; // Display elements of myhash2 Console.WriteLine( "Elements of myhash2:" ); foreach ( var value in myhash2) { Console.WriteLine(value); } } } |
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000
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.
Important Points Related To HashSet in C#
- 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.
- It 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.