EN
C# / .NET - clear HashSet
0 points
In this article, we would like to show you how to clear HashSet in C#.
Quick solution:
xxxxxxxxxx
1
HashSet<string> myHashset = new HashSet<string> { "A", "B", "C" };
2
3
myHashset.Clear();
In this example, we use Clear()
method to remove all items from myHashset
.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
9
HashSet<string> myHashset = new HashSet<string> { "A", "B", "C" };
10
11
Console.WriteLine("Before:");
12
foreach (string item in myHashset)
13
Console.WriteLine(item);
14
15
myHashset.Clear(); // clear HashSet
16
17
Console.WriteLine("\nAfter:");
18
foreach (string item in myHashset)
19
Console.WriteLine(item);
20
21
}
22
}
Output:
xxxxxxxxxx
1
Before:
2
A
3
B
4
C
5
6
After: