C# | Remove all elements in a collection from a HashSet Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 elements in a collection from a HashSet: C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { HashSet<int> set1 = new HashSet<int> { 1, 2, 3, 4, 5 }; HashSet<int> set2 = new HashSet<int> { 2, 4 }; Console.WriteLine("Before removing elements from set1:"); foreach (int num in set1) { Console.Write(num + " "); } Console.WriteLine(); set1.ExceptWith(set2); Console.WriteLine("After removing elements from set1:"); foreach (int num in set1) { Console.Write(num + " "); } Console.WriteLine(); } } OutputBefore removing elements from set1: 1 2 3 4 5 After removing elements from set1: 1 3 5 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.ExceptWith(IEnumerable) Method is used to remove all elements in the specified collection from the current HashSet object. Syntax: mySet2.ExceptWith(mySet1) Here, mySet1 and mySet2 are the two HashSets and the function returns the elements of mySet2 which are not in mySet1. Exception: This method will give ArgumentNullException if the HashSet is null. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all elements // in a collection from a HashSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of integers HashSet<int> mySet1 = new HashSet<int>(); // Inserting elements into HashSet mySet1 for (int i = 0; i < 7; i++) { mySet1.Add(i); } // Creating a HashSet of integers HashSet<int> mySet2 = new HashSet<int>(); // Inserting elements into HashSet mySet2 for (int i = 4; i < 11; i++) { mySet2.Add(i); } // Removing all elements in a collection from a HashSet mySet2.ExceptWith(mySet1); // Printing the elements in mySet2 // It only prints the elements which are // in mySet2 and not in mySet1 foreach(int i in mySet2) { Console.WriteLine(i); } } } Example 2: CSHARP // C# code to remove all elements // in a collection from a HashSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of strings HashSet<string> mySet1 = new HashSet<string>(); // Inserting elements into HashSet mySet1 mySet1.Add("Punjab"); mySet1.Add("Haryana"); mySet1.Add("Jammu"); mySet1.Add("Himachal"); mySet1.Add("Delhi"); // Displaying all elements in mySet1 Console.WriteLine("The elements in mySet1 are : "); foreach(string i in mySet1) { Console.WriteLine(i); } // Creating a HashSet of strings HashSet<string> mySet2 = new HashSet<string>(); // Inserting elements into HashSet mySet2 mySet2.Add("Bangalore"); mySet2.Add("Kerala"); mySet2.Add("Uttar Pradesh"); mySet2.Add("Himachal"); mySet2.Add("Delhi"); // Displaying all elements in mySet2 Console.WriteLine("The elements in mySet2 are : "); foreach(string i in mySet2) { Console.WriteLine(i); } // Removing all elements in a collection from a HashSet mySet2.ExceptWith(mySet1); // Printing the elements in mySet2 // It only prints the elements which are // in mySet2 and not in mySet1 Console.WriteLine("The elements in mySet2 are : "); foreach(string i in mySet2) { Console.WriteLine(i); } } } Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.exceptwith?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove elements from a HashSet with conditions defined by the predicate S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads 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# | Remove all elements from the Collection<T> Collection<T>.Clear method is used to remove all elements from the Collection<T>. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all // elements from the Collection using System; using S 2 min read C# | Remove all elements from the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Clear Method is used to remove all elements from the Hashtable. Syntax: myTable.Clear() Here myTable is the name o 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# | Remove elements from a HashSet with conditions defined by the predicate 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. HashSet 3 min read C# | Create HashSet from another collection 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 intege 2 min read Like