VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-create-hashset-from-another-collection/

⇱ C# | Create HashSet from another collection - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Create HashSet from another collection

Last Updated : 22 Feb, 2023

In C#, you can create a HashSet<T> from another collection by using the HashSet<T> constructor that takes an IEnumerable<T> parameter. Here's an example:


Output
Contents of HashSet:
1
2
3
4
5

A HashSet is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. You can create a HashSet from another collection by passing the collection as an argument while creating the object of HashSet. Below given are some examples to understand the implementation in a better way: Example 1: 

Example 2: 

Comment

Explore