C# HashSet IntersectWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In C#, HashSet.IntersectWith(other) keeps only the items that are in both the set and other. In Venn diagram words, it keeps the part where the two circles overlap and throws away the rest.

With A = {1, 2, 3} and B = {3, 4, 5}, the call A.IntersectWith(B) leaves A = {3}. The set A is changed in place; items are removed from it. B is only read, and the method returns nothing.

This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.IntersectWith(...). 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 IntersectWith on the set you want to shrink. The argument can be any IEnumerable<T>.

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

a.IntersectWith(b);      // a is now {3}
// a.IntersectWith(new int[0]) would leave a empty
CallWhat it does
a.IntersectWith(b)Removes from a every item that is not in b.
a.IntersectWith(new int[0])Intersecting with an empty collection leaves a empty.
new HashSet<int>(a).IntersectWith(b)Copy first when you still need the original a.

If no item is shared, the set becomes empty. Intersecting a set with itself changes nothing. Passing null throws ArgumentNullException. Repeats inside other do not matter.

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

Parameters

ParameterDescription
otherRequired. The collection to compare with. Only items found in this collection stay in the set. It must not be null.

Return Value

Return ValueDescription
voidNothing is returned. The set you called it on now holds only the shared items.

C# Examples

Example 1 — Keep the Shared Number

Intersect {1, 2, 3} with {3, 4, 5} and see which item survives.

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

        Console.WriteLine(Show(a));
        Console.WriteLine(a.Count);
        Console.WriteLine(Show(b));
    }
}

How It Works

  1. Overlap of two circles. The only number in both a and b is 3.
  2. Others are removed from a. 1 and 2 are not in b, so IntersectWith deletes them from a.
  3. One item left. a is {3} and Count is 1. The 4 and 5 never join a, because intersection only removes.
  4. b is only read. It still prints {3, 4, 5}. Show sorts a copy before printing, so the order is stable.

Example 2 — No Shared Items and Empty Argument

Intersect with a set that shares nothing, and with an empty array, to see the set become empty.

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 };
        a.IntersectWith(new int[] { 8, 9 });
        Console.WriteLine(Show(a));
        Console.WriteLine(a.Count);

        HashSet<int> b = new HashSet<int> { 1, 2, 3 };
        b.IntersectWith(new int[0]);
        Console.WriteLine(b.Count);

        HashSet<int> c = new HashSet<int> { 4, 5 };
        c.IntersectWith(c);
        Console.WriteLine(Show(c));
    }
}

How It Works

  1. Circles that do not touch. {1, 2} and {8, 9} share nothing, so every item of a is removed and Show prints {}.
  2. Empty argument. An empty collection has no shared items with anything, so b also ends up with Count 0.
  3. Safe to repeat. Nothing breaks when the result is empty. You can ask for Count, or use the set again with Add.
  4. With itself. Every item of c is also in c, so it keeps all of them and stays {4, 5}.

Example 3 — Find Students Who Came on Both Days

Copy the Monday set, intersect it with Tuesday's list, and keep Monday's original set intact.

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

class Program {
    static void Main() {
        HashSet<string> monday = new HashSet<string> { "Asha", "Ravi", "Meena" };
        string[] tuesday = { "Ravi", "Meena", "Karan" };

        HashSet<string> both = new HashSet<string>(monday);
        both.IntersectWith(tuesday);

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

        Console.WriteLine("Both days: " + string.Join(", ", names));
        Console.WriteLine("Monday still has: " + monday.Count);
    }
}

How It Works

  1. Copy before changing. new HashSet<string>(monday) makes a separate set, so the intersection will not damage Monday's list.
  2. Array as the other side. tuesday is a plain array. IntersectWith accepts it because an array is an IEnumerable<string>.
  3. Who stays. "Ravi" and "Meena" are in both lists. "Asha" is removed, and "Karan" is never added.
  4. Sort for stable output. The names are copied to a list and sorted, giving Meena, Ravi. The last line shows that monday still has 3 names.

Frequently Asked Questions

It keeps only the items that are in both the set and the other collection, and removes everything else from the set.
No. It changes the set you call it on and returns nothing. Copy the set first with new HashSet<T>(set) when you need the original.
The set becomes empty. Count is 0 and you can still use the set afterward.
Use Overlaps. It returns true when at least one item is shared and does not change anything.
No. Any IEnumerable<T> works, such as an array or a List. Passing a HashSet with the same comparer is usually the fastest.

Next: IsProperSubsetOf()

Check whether the HashSet fits inside another collection that also has at least one more item.

IsProperSubsetOf() 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