
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Remove all elements from a HashSet in C#
To remove all elements from a HashSet, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ HashSet<string> set1 = new HashSet<string>(); set1.Add("A"); set1.Add("B"); set1.Add("C"); set1.Add("D"); set1.Add("E"); set1.Add("F"); set1.Add("G"); set1.Add("H"); Console.WriteLine("Elements in HashSet1..."); foreach (string res in set1){ Console.WriteLine(res); } HashSet<string> set2 = new HashSet<string>(); set2.Add("John"); set2.Add("Jacob"); set2.Add("Ryan"); set2.Add("Tom"); set2.Add("Andy"); set2.Add("Tim"); set2.Add("Steve"); set2.Add("Mark"); Console.WriteLine("Elements in HashSet2..."); foreach (string res in set2){ Console.WriteLine(res); } Console.WriteLine("Is HashSet1 equal to HashSet2? = "+set1.Equals(set2)); Console.WriteLine("Count of HashSet2 = "+set2.Count); set2.Clear(); Console.WriteLine("Count of HashSet2 (updated) = "+set2.Count); } }
Output
This will produce the following output −
Elements in HashSet1... A B C D E F G H Elements in HashSet2... John Jacob Ryan Tom Andy Tim Steve Mark Is HashSet1 equal to HashSet2? = False Count of HashSet2 = 8 Count of HashSet2 (updated) = 0
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ HashSet<string> set1 = new HashSet<string>(); set1.Add("A"); set1.Add("B"); set1.Add("C"); set1.Add("D"); set1.Add("E"); set1.Add("F"); set1.Add("G"); set1.Add("H"); Console.WriteLine("Elements in HashSet..."); foreach (string res in set1){ Console.WriteLine(res); } Console.WriteLine("Count of HashSet1 = "+set1.Count); set1.Clear(); Console.WriteLine("Count of HashSet1 (updated) = "+set1.Count); } }
Output
This will produce the following output −
Elements in HashSet... A B C D E F G H Count of HashSet1 = 8 Count of HashSet1 (updated) = 0
- Related Articles
- Remove all elements from a HashSet in Java
- Remove all elements in a collection from a HashSet in C#
- Remove elements from a HashSet with conditions defined by the predicate in C#
- Remove all elements from a SortedList in C#
- Remove all elements from OrderedDictionary in C#
- Remove the specified element from a HashSet in C#
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- Remove duplicate elements in Java with HashSet
- Add all the elements from a collection to the HashSet in Java
- Remove single element from a HashSet in Java
- Remove all elements from TreeSet in Java
- Remove all elements from Java NavigableMap

Advertisements