C# | Remove all elements from a HashSet Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. HashSet.Clear Method is used to remove all the elements from a HashSet object. Syntax: mySet.Clear(); Here, mySet is the name of the HashSet. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all elements from HashSet 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 20 in HashSet mySet1 for (int i = 0; i < 10; i++) { mySet.Add(i * 2); } // Displaying the number of elements in HashSet Console.WriteLine(mySet.Count); // Removing all the elements from HashSet mySet.Clear(); // Displaying the number of elements in HashSet // after removing all the elements from it Console.WriteLine(mySet.Count); } } Output: 10 0 Example 2: CSHARP // C# code to remove all elements from HashSet 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 in HashSet mySet.Add("Geeks"); mySet.Add("and"); mySet.Add("GeeksforGeeks"); mySet.Add("are"); mySet.Add("the"); mySet.Add("best"); // Displaying the number of elements in HashSet Console.WriteLine(mySet.Count); // Removing all the elements from HashSet mySet.Clear(); // Displaying the number of elements in HashSet // after removing all the elements from it Console.WriteLine(mySet.Count); // Inserting elements in HashSet mySet.Add("Join"); mySet.Add("Geeks"); mySet.Add("Classes"); // Displaying the number of elements in HashSet Console.WriteLine(mySet.Count); } } Output: 6 0 3 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.clear?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove the specified element from a HashSet 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 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# | 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 the specified element from a HashSet 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# | Remove all elements from a SortedList SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Clear method is used to remove all the elements from a SortedList ob 2 min read C# | Add element to HashSet 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. Elements 2 min read C# | Remove all elements from the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements. 2 min read Like