In C#, HashSet.Clear() throws away every item in the set at once. After the call, Count is 0 and Contains answers false for everything.
The set object itself is still alive and ready to use, so you can reuse one set for many rounds of work. Clear keeps the memory the set already reserved (its capacity), which is handy for reuse. Call TrimExcess() if you want that space back.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.Clear(...). A HashSet<T> is a class, so two variables can share one set. It keeps unique items only and does not promise any order.
Syntax
Call Clear with no arguments. It returns nothing.
C#
HashSet<int> numbers = new HashSet<int> { 1, 2, 3 };
numbers.Clear();
// numbers.Count is 0, numbers.Contains(1) is false
numbers.Add(5); // the same set can be used again
Call
What it does
numbers.Clear()
Removes all items. Does nothing harmful when the set is already empty.
numbers = new HashSet<int>()
Not the same: this makes a new set and leaves the old one alone for other variables.
numbers.Count == 0
Check this to see that a set is empty, before or after Clear.
Clear is an O(n) operation, because it releases every stored item. It never throws for an empty set. Other variables that point to the same set object see the change too.
Library declarations
C#
public void Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. Afterward Count is 0.
C# Examples
Example 1 — Clear a Set and Use It Again
Fill a set, clear it, check that it is empty, then add a new item to the same set.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> numbers = new HashSet<int> { 1, 2, 3 };
Console.WriteLine(numbers.Count);
numbers.Clear();
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.Contains(1));
Console.WriteLine(numbers.Add(5));
Console.WriteLine(numbers.Count);
}
}
Output
3
0
False
True
1
How It Works
Three items first. The initializer stores 1, 2, and 3, so the first Count is 3.
One call empties it. After Clear(), Count is 0 and Contains(1) is False.
Still usable.Clear does not destroy the set object. Add(5) works and returns True, because the set has no 5.
Fresh start. The set now holds one item, so the last Count is 1.
Example 2 — Clear a Shared Set vs a Copy
Make a second variable for the same set and a separate copy, then clear the original.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> original = new HashSet<int> { 1, 2, 3 };
HashSet<int> alias = original;
HashSet<int> copy = new HashSet<int>(original);
original.Clear();
Console.WriteLine(original.Count);
Console.WriteLine(alias.Count);
Console.WriteLine(copy.Count);
Console.WriteLine(ReferenceEquals(original, alias));
}
}
Output
0
0
3
True
How It Works
Alias means same object.alias = original copies the reference, not the items. Both names point to one set.
Copy means new object.new HashSet<int>(original) builds a separate set that holds its own 1, 2, and 3.
Clear the original. Both original and alias now show Count 0, because they are the same set.
The copy survives.copy.Count is still 3. ReferenceEquals confirms that original and alias are one object.
Example 3 — Count Unique Items Per Batch
Use one set for several batches of numbers. Clear it between batches so earlier items do not leak into the next count.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
int[][] batches = {
new int[] { 1, 1, 2 },
new int[] { 7, 7, 7 },
new int[] { 4, 5, 6, 4 }
};
HashSet<int> seen = new HashSet<int>();
int round = 1;
foreach (int[] batch in batches) {
seen.Clear();
foreach (int n in batch) {
seen.Add(n);
}
Console.WriteLine("Batch " + round + ": " + seen.Count + " unique");
round++;
}
}
}
Jagged array.int[][] is an array of arrays, so each inner array is one batch of numbers.
Clear first.seen.Clear() runs at the start of every batch, so the set always begins empty.
Duplicates collapse. The inner loop adds every number, and repeats are ignored: {1, 1, 2} gives 2 items and {7, 7, 7} gives 1.
Without Clear. If you removed the Clear call, batch 3 would also remember 1, 2, and 7 from earlier batches and print a larger count.
Frequently Asked Questions
It removes all items from the set, so Count becomes 0. The set object stays usable and you can add new items afterward.
No. Clear keeps the memory the set has already reserved, so refilling it is cheap. Call TrimExcess to shrink the unused space.
No. Clear empties the existing object, and every variable that points to it sees the change. Assigning new HashSet makes a different object and leaves the old one untouched.
No. Calling Clear on an empty set simply does nothing.
Compare Count with 0. HashSet has no IsEmpty property, so use set.Count == 0.