In C#, HashSet.ExceptWith(other)subtracts one collection from the set. Every item that is also in other is removed. In Venn diagram words: keep only the part of your circle that does not touch the other circle.
With A = {1, 2, 3} and B = {3, 4, 5}, the call A.ExceptWith(B) removes 3 from A and leaves A = {1, 2}. The 4 and 5 are not in A, so they are simply ignored. The set A is changed in place and B is untouched.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.ExceptWith(...). 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 ExceptWith on the set you want to trim. The argument lists the items to take away.
C#
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
a.ExceptWith(b); // a is now {1, 2}
// items in b that a never had (4, 5) are ignored
Call
What it does
a.ExceptWith(b)
Removes from a every item that is also in b.
a.ExceptWith(a)
Subtracting a set from itself removes everything, so a becomes empty.
a.ExceptWith(new int[0])
Subtracting an empty collection changes nothing.
Items in other that are not in the set are ignored with no error. Passing null throws ArgumentNullException. If you only want to delete by a rule instead of by a list, use RemoveWhere().
Library declarations
C#
public void ExceptWith(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection of items to remove from the set. It must not be null. Repeats are fine.
Return Value
Return Value
Description
void
Nothing is returned. The set you called it on no longer holds any item from other.
C# Examples
Example 1 — Take One Set Away From Another
Subtract {3, 4, 5} from {1, 2, 3}, then try the other direction on a fresh pair.
C#
using System;
using System.Collections.Generic;
class Program {
static string Show(HashSet<int> set) {
List<int> items = new List<int>(set);
items.Sort();
return "{" + string.Join(", ", items) + "}";
}
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
a.ExceptWith(b);
Console.WriteLine(Show(a));
HashSet<int> c = new HashSet<int> { 1, 2, 3 };
HashSet<int> d = new HashSet<int> { 3, 4, 5 };
d.ExceptWith(c);
Console.WriteLine(Show(d));
}
}
Output
{1, 2}
{4, 5}
How It Works
Cut the overlap. The only item a shares with b is 3, so that one item is removed from a.
4 and 5 are ignored. They are not in a, so there is nothing to remove for them, and no error happens.
Other direction. The sets c and d are fresh copies. d.ExceptWith(c) removes 3 from d, leaving {4, 5}.
Not symmetric. The two answers differ because the set on the left of the dot is the one being trimmed. Show sorts the items for a stable print.
Example 2 — Subtract Empty and Subtract Itself
Subtract an empty array, then subtract the set from itself to see it clear.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
a.ExceptWith(new int[0]);
Console.WriteLine(a.Count);
a.ExceptWith(new int[] { 7, 8 });
Console.WriteLine(a.Count);
a.ExceptWith(a);
Console.WriteLine(a.Count);
Console.WriteLine(a.Contains(1));
}
}
Output
3
3
0
False
How It Works
Nothing to subtract. An empty array removes no items, so Count stays 3.
Strangers are ignored. 7 and 8 were never in the set, so Count is still 3. A set never complains about missing items here.
Itself. Every item of a is also in a, so a.ExceptWith(a) removes them all and Count is 0.
Final check. After that, Contains(1) is False. This acts like Clear(), only less obvious to read.
Example 3 — Remove Blocked Addresses From an Inbox
Keep a set of addresses and subtract a blocklist array. Addresses on the blocklist that are not in the inbox are ignored.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> inbox = new HashSet<string> { "[email protected]", "[email protected]", "[email protected]" };
string[] blocked = { "[email protected]", "[email protected]", "[email protected]" };
inbox.ExceptWith(blocked);
List<string> rest = new List<string>(inbox);
rest.Sort();
Console.WriteLine(inbox.Count);
Console.WriteLine(string.Join(", ", rest));
Console.WriteLine(inbox.Contains("[email protected]"));
}
}