In C#, HashSet.Remove(item) takes one item out of the set. If you crossed a name off the guest list, this is the call that does it.
It returns true when the item was in the set and is now gone, and false when the item was never there. Removing a missing item is not an error, so you do not need to call Contains() first. To remove many items by a rule, use RemoveWhere().
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.Remove(...). 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 Remove with the item you want to delete. The result tells you whether something was removed.
C#
HashSet<int> numbers = new HashSet<int> { 10, 20, 30 };
bool a = numbers.Remove(20); // true: 20 was in the set
bool b = numbers.Remove(20); // false: it is gone now
bool c = numbers.Remove(99); // false: it was never there
Call
What it does
numbers.Remove(20)
Deletes 20 and returns true when it was found.
numbers.Remove(99)
Returns false and leaves the set alone when 99 is missing.
numbers.Remove(x) ? "gone" : "none"
Use the bool to pick a message or to count real removals.
Remove is an average O(1) call and uses the set's comparer to find the item. After a successful call Count goes down by 1. It does not shrink the memory the set holds; call TrimExcess() for that.
Library declarations
C#
public bool Remove(T item);
Parameters
Parameter
Description
item
Required. The item to remove. The set finds it with its comparer, so with StringComparer.OrdinalIgnoreCase a different spelling still matches.
Return Value
Return Value
Description
bool
true if the item was found and removed. false if the set did not contain it.
C# Examples
Example 1 — Remove a Number Twice
Remove an item that exists, remove it again, then try one that never existed.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> numbers = new HashSet<int> { 10, 20, 30 };
Console.WriteLine(numbers.Remove(20));
Console.WriteLine(numbers.Remove(20));
Console.WriteLine(numbers.Remove(99));
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.Contains(20));
}
}
Output
True
False
False
2
False
How It Works
Initial set. The collection initializer stores 10, 20, and 30, so Count starts at 3.
First Remove. The value 20 is in the set, so it is deleted and the call returns True.
Second and third Remove. 20 is already gone and 99 was never added. Both calls return False and no exception is thrown.
Final state. Only one real removal happened, so Count is 2 and Contains(20) is False.
Example 2 — Remove With a Different Spelling
Use a set that ignores case, then remove a name using capital letters.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> team = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
team.Add("Asha");
team.Add("Ravi");
Console.WriteLine(team.Remove("ASHA"));
Console.WriteLine(team.Contains("asha"));
Console.WriteLine(team.Count);
HashSet<string> strict = new HashSet<string> { "Asha", "Ravi" };
Console.WriteLine(strict.Remove("ASHA"));
Console.WriteLine(strict.Count);
}
}
Output
True
False
1
False
2
How It Works
Comparer picks the match. The team set was built with StringComparer.OrdinalIgnoreCase, so "ASHA" counts as the stored "Asha".
Removed and gone.Remove("ASHA") returns True, so Contains("asha") is now False and one name is left.
Strict set. The second set uses the default comparer, where letter case matters. "ASHA" is not "Asha", so Remove returns False.
Nothing deleted. That is why strict.Count is still 2.
Example 3 — Tick Off Finished Tasks
Keep pending tasks in a set and remove each one as it is finished. The result of Remove shows which names were real tasks.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> pending = new HashSet<string> { "email", "report", "backup" };
string[] done = { "report", "lunch", "email" };
foreach (string task in done) {
if (pending.Remove(task)) {
Console.WriteLine("Finished: " + task);
} else {
Console.WriteLine("Not pending: " + task);
}
}
Console.WriteLine(pending.Count);
Console.WriteLine(pending.Contains("backup"));
}
}
Output
Finished: report
Not pending: lunch
Finished: email
1
True
How It Works
Set as a to-do list.pending holds the three open tasks. A set fits here because each task name should appear once.
Array drives the loop. The done array has a fixed order, so the messages always print in that order, no matter how the set stores its items.
Remove as a question.pending.Remove(task) is the if condition. "report" and "email" are removed, while "lunch" was never a task and takes the else branch.
What is left. Two tasks were removed, so one remains, and "backup" is still pending.
Frequently Asked Questions
It returns true if the item was found and removed, and false if the set did not contain the item.
No. It just returns false. You do not need to check with Contains first.
Use RemoveWhere with a rule to delete every matching item, ExceptWith to delete everything that is in another collection, or Clear to delete everything.
Avoid it. Newer .NET versions tolerate Remove during a loop, but older ones throw InvalidOperationException, and adding items always breaks the loop. RemoveWhere is the safe choice.
Not right away. The set keeps its internal space after Remove. Call TrimExcess if you want to give unused space back.