C# HashSet Overlaps() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In C#, HashSet.Overlaps(other) asks one simple question: do the set and other have at least one item in common? It returns true or false. In Venn diagram words: do the two circles touch?

It is the lightweight cousin of IntersectWith(). IntersectWith changes the set to keep only shared items. Overlaps only answers and changes nothing, and it can stop as soon as it finds one shared item.

This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.Overlaps(...). 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 Overlaps on a set and pass any collection to compare with.

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

bool x = a.Overlaps(new int[] { 3, 4, 5 });   // true: they share 3
bool y = a.Overlaps(new int[] { 8, 9 });      // false: nothing in common
bool z = a.Overlaps(new int[0]);              // false: empty has nothing to share
CallWhat it does
a.Overlaps(b)true if at least one item is in both a and b.
a.Overlaps(new int[0])false: an empty collection shares nothing.
emptySet.Overlaps(anything)false: an empty set has no item to share.

Passing null throws ArgumentNullException. The answer is the same in both directions: a.Overlaps(b) equals b.Overlaps(a) for sets with the same comparer. Neither set is changed.

Library declarations
C#
public bool Overlaps(IEnumerable<T> other);

Parameters

ParameterDescription
otherRequired. The collection to look for shared items in. It must not be null.

Return Value

Return ValueDescription
booltrue if the set and other have at least one item in common. false if they share nothing, or if either one is empty.

C# Examples

Example 1 — Do These Sets Touch?

Compare {1, 2, 3} with a list that shares 3, a list that shares nothing, and an empty list.

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

class Program {
    static void Main() {
        HashSet<int> a = new HashSet<int> { 1, 2, 3 };

        Console.WriteLine(a.Overlaps(new int[] { 3, 4, 5 }));
        Console.WriteLine(a.Overlaps(new int[] { 8, 9 }));
        Console.WriteLine(a.Overlaps(new int[0]));
        Console.WriteLine(a.Count);
    }
}

How It Works

  1. Touching circles. The array and a both hold the number 3, so one shared item is enough for True.
  2. No contact. 8 and 9 are not in a, so the answer is False.
  3. Empty has nothing to share. An empty array cannot share any item, so the third line is False.
  4. Read-only. Unlike IntersectWith, Overlaps leaves a alone, so Count is still 3.

Example 2 — Empty Set, Itself, and Direction

See that an empty set never overlaps, that a non-empty set overlaps itself, and that the order of the two sets does not change the answer.

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

class Program {
    static void Main() {
        HashSet<int> a = new HashSet<int> { 1, 2, 3 };
        HashSet<int> b = new HashSet<int> { 3, 4 };
        HashSet<int> empty = new HashSet<int>();

        Console.WriteLine(empty.Overlaps(a));
        Console.WriteLine(empty.Overlaps(empty));
        Console.WriteLine(a.Overlaps(a));
        Console.WriteLine(a.Overlaps(b) == b.Overlaps(a));
    }
}

How It Works

  1. Empty set. It has no item that could be shared, so empty.Overlaps(a) is False.
  2. Empty with empty. Two empty sets share nothing either, so the answer is still False. Equal does not mean overlapping here.
  3. Set with itself. Every item of a is in a, so a non-empty set always overlaps itself: True.
  4. Direction does not matter. The line compares a.Overlaps(b) with b.Overlaps(a) and prints True, because both give the same answer.

Example 3 — Detect a Meeting Conflict

Store busy hours in a set and use Overlaps to test two proposed meetings.

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

class Program {
    static void Main() {
        HashSet<int> busy = new HashSet<int> { 9, 10, 14 };

        int[] slotA = { 10, 11 };
        int[] slotB = { 12, 13 };

        Console.WriteLine("Slot A conflicts: " + busy.Overlaps(slotA));
        Console.WriteLine("Slot B conflicts: " + busy.Overlaps(slotB));
        Console.WriteLine("Busy hours: " + busy.Count);
    }
}

How It Works

  1. Hours as numbers. The set busy holds 9, 10, and 14, meaning 9 AM, 10 AM, and 2 PM. A set is a good fit because an hour is either busy or free.
  2. Slot A. It needs hours 10 and 11. Hour 10 is busy, so the two collections share an item and Overlaps returns True.
  3. Slot B. Hours 12 and 13 are both free, so nothing is shared and the answer is False.
  4. Schedule not changed. Asking only reads the set. busy.Count is still 3 after both checks.

Frequently Asked Questions

It returns true if the set and the other collection have at least one item in common, and false if they share nothing.
IntersectWith changes the set so it keeps only the shared items. Overlaps only returns true or false and changes nothing.
No. An empty set has no items, so Overlaps always returns false for it, even against another empty set.
No. Overlaps needs just one shared item. IsSupersetOf needs the set to hold every item of the other collection.
Yes. Any IEnumerable<T> works. A HashSet is not required, though a HashSet with the same comparer is usually the fastest.

Next: Remove()

Remove one item from a HashSet; returns true if it was there and false if it was not.

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

10 people found this page helpful