C# HashSet SymmetricExceptWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In C#, HashSet.SymmetricExceptWith(other) keeps the items that are in one of the two collections but not in both. Programmers also call it the "XOR" of two sets. In Venn diagram words: keep both circles, but cut out the middle part where they overlap.

With A = {1, 2, 3} and B = {3, 4, 5}, the call A.SymmetricExceptWith(B) turns A into {1, 2, 4, 5}. The shared 3 is removed, and the items only found in B (4 and 5) are added. 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.SymmetricExceptWith(...). 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 SymmetricExceptWith on the set you want to turn into the "different items" set.

C#
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };

a.SymmetricExceptWith(b);   // a is now {1, 2, 4, 5}
a.SymmetricExceptWith(b);   // do it again: a is back to {1, 2, 3}
CallWhat it does
a.SymmetricExceptWith(b)Removes shared items from a and adds the items only found in b.
a.SymmetricExceptWith(a)Everything is shared, so a becomes empty.
a.SymmetricExceptWith(new int[0])Nothing to compare, so a stays as it is.

If two sets hold exactly the same items, the result is empty. Repeats inside other are treated as one item. Passing null throws ArgumentNullException.

Library declarations
C#
public void SymmetricExceptWith(IEnumerable<T> other);

Parameters

ParameterDescription
otherRequired. The collection to compare with. Items found only here are added to the set, and items found in both are removed from the set. It must not be null.

Return Value

Return ValueDescription
voidNothing is returned. The set you called it on now holds only the items that were in exactly one of the two collections.

C# Examples

Example 1 — Keep the Items That Differ

Apply SymmetricExceptWith to {1, 2, 3} and {3, 4, 5} and check the new set.

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.SymmetricExceptWith(b);

        Console.WriteLine(Show(a));
        Console.WriteLine(a.Count);
        Console.WriteLine(a.Contains(3));
    }
}

How It Works

  1. Compare the circles. 1 and 2 live only in a. 4 and 5 live only in b. The number 3 lives in both.
  2. Shared item goes. The 3 is in both sets, so SymmetricExceptWith removes it from a.
  3. New items come in. The items found only in b (4 and 5) are added to a. That is the part ExceptWith never does.
  4. Result. a holds four items, Count is 4, and Contains(3) is False. Show sorts a copy before printing.

Example 2 — Apply It Twice to Get Back to Start

Run the same call two times with the same second set. The second run undoes the first.

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.SymmetricExceptWith(b);
        Console.WriteLine(Show(a));

        a.SymmetricExceptWith(b);
        Console.WriteLine(Show(a));

        a.SymmetricExceptWith(a);
        Console.WriteLine(a.Count);
    }
}

How It Works

  1. First pass. The shared 3 is dropped, and 4 and 5 are added, giving {1, 2, 4, 5}.
  2. Second pass. Now 4 and 5 are in both sets, so they are removed. The number 3 is only in b, so it is added back.
  3. Back to the start. The set is {1, 2, 3} again. Like switching a light on and off, two calls cancel out.
  4. With itself. Every item is shared with itself, so a.SymmetricExceptWith(a) leaves an empty set and Count is 0.

Example 3 — Find Files Added or Deleted

Compare the old file list with the new one. Anything in only one list was either deleted or created.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        HashSet<string> oldFiles = new HashSet<string> { "a.txt", "b.txt", "c.txt" };
        string[] newFiles = { "b.txt", "c.txt", "d.txt", "d.txt" };

        HashSet<string> changed = new HashSet<string>(oldFiles);
        changed.SymmetricExceptWith(newFiles);

        List<string> names = new List<string>(changed);
        names.Sort();

        Console.WriteLine("Changed: " + string.Join(", ", names));
        Console.WriteLine(changed.Count);
        Console.WriteLine(oldFiles.Count);
    }
}

How It Works

  1. Copy the old list. new HashSet<string>(oldFiles) makes a working set, so oldFiles keeps its three names.
  2. Shared files vanish. b.txt and c.txt are in both lists, so they are not "changed" and are removed from the working set.
  3. Only-in-one files stay or arrive. a.txt was only in the old list. d.txt is only in the new list, and its repeat in the array counts once.
  4. Stable print. The two changed names are sorted before printing. changed.Count is 2 and oldFiles.Count is still 3.

Frequently Asked Questions

It changes the set so it holds only the items that are in exactly one of the two collections. Items found in both are removed, and items found only in the other collection are added.
ExceptWith only removes items. SymmetricExceptWith removes the shared items and also adds the items that exist only in the other collection.
An empty set. Every item is shared, so every item is removed and nothing new is added.
Yes. Repeats in the other collection count as one item, so a repeated name is added only once.
Copy the set first, for example new HashSet<int>(a), and call SymmetricExceptWith on the copy.

Next: TrimExcess()

Give back unused space after a HashSet shrinks; the items stay the same.

TrimExcess() 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.

14 people found this page helpful