C# | Create HashSet from another collection Last Updated : 22 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: C# using System; using System.Collections.Generic; public class Program { public static void Main() { // create a List of integers List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; // create a HashSet from the List HashSet<int> numberSet = new HashSet<int>(numbers); // print out the contents of the HashSet using a foreach loop Console.WriteLine("Contents of HashSet:"); foreach (int number in numberSet) { Console.WriteLine(number); } } } OutputContents 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: CSHARP // C# code to Create HashSet // from another collection using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of integers HashSet<int> mySet = new HashSet<int>(); // Inserting even numbers less than // equal to 10 in HashSet mySet for (int i = 0; i < 5; i++) { mySet.Add(i * 2); } // Creating new HashSet mySet_new from already // Created HashSet mySet HashSet<int> mySet_new = new HashSet<int>(mySet); Console.WriteLine("The elements in newly created HashSet are : "); // Displaying the elements of newly created HashSet foreach(int i in mySet_new) { Console.WriteLine(i); } } } Example 2: CSHARP // C# code to Create HashSet // from another collection using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of strings HashSet<string> mySet = new HashSet<string>(); // Inserting elements into HashSet mySet mySet.Add("Delhi"); mySet.Add("Noida"); mySet.Add("Chandigarh"); mySet.Add("New York"); mySet.Add("Bangalore"); // Creating new HashSet mySet_new from already // Created HashSet mySet HashSet<string> mySet_new = new HashSet<string>(mySet); Console.WriteLine("The elements in newly created HashSet are : "); // Displaying the elements of newly created HashSet foreach(string i in mySet_new) { Console.WriteLine(i); } } } Comment More infoAdvertise with us Next Article C# | Create HashSet from another collection S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Generic-HashSet CSharp-Generic-Namespace Similar Reads C# | Remove all elements in a collection from a HashSet In C#, you can use the ExceptWith() method to remove all the elements in a collection from a HashSet. The ExceptWith() method removes all the elements in the specified collection from the current HashSet. Here is an example code that demonstrates how to use the ExceptWith() method to remove all the 3 min read C# | Remove all elements from a HashSet A HashSet is an unordered collection of the unique elements. It comes under the 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. HashS 2 min read C# | Check if a HashSet and a specified collection share common elements In C#, you can use the SetEquals method to check if a HashSet and a specified collection contain the same elements. The SetEquals method returns true if the HashSet and the specified collection contain the same elements; otherwise, it returns false. Here is an example code that demonstrates how to u 3 min read C# | Check if a HashSet is a subset of the specified collection A HashSet is an unordered collection of the unique elements. It is found in 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. HashSet.I 2 min read C# | Check if HashSet and the specified collection contain the same elements Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet<int> 3 min read Like