C# HashSet ExceptWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

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
CallWhat 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

ParameterDescription
otherRequired. The collection of items to remove from the set. It must not be null. Repeats are fine.

Return Value

Return ValueDescription
voidNothing 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));
    }
}

How It Works

  1. Cut the overlap. The only item a shares with b is 3, so that one item is removed from a.
  2. 4 and 5 are ignored. They are not in a, so there is nothing to remove for them, and no error happens.
  3. Other direction. The sets c and d are fresh copies. d.ExceptWith(c) removes 3 from d, leaving {4, 5}.
  4. 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));
    }
}

How It Works

  1. Nothing to subtract. An empty array removes no items, so Count stays 3.
  2. Strangers are ignored. 7 and 8 were never in the set, so Count is still 3. A set never complains about missing items here.
  3. Itself. Every item of a is also in a, so a.ExceptWith(a) removes them all and Count is 0.
  4. 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]"));
    }
}

How It Works

  1. Data as sets. The inbox is a set, since each address should appear once. The blocklist is only an array and has a repeat.
  2. Subtract the list. [email protected] is in both, so it is removed. The repeat in the array makes no difference.
  3. Stranger ignored. [email protected] was never in the inbox, so it is skipped quietly.
  4. Stable output. Two addresses remain. They are sorted in a list before printing, and Contains("[email protected]") confirms the blocked one is gone.

Frequently Asked Questions

It removes from the set every item that is also in the other collection. This is the set difference, or subtraction.
No. Those items are ignored. Only a null argument throws, with ArgumentNullException.
ExceptWith removes the items found in another collection. RemoveWhere removes items that pass a test you write as a lambda.
It removes everything, so the set becomes empty. It has the same effect as Clear.
No. Each call trims the set on the left side of the dot. For A = {1,2,3} and B = {3,4,5} you get {1,2} and {4,5}.

Next: GetEnumerator()

Get an enumerator to walk through a HashSet; foreach calls it for you.

GetEnumerator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful