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(). IntersectWithchanges 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
Call
What 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
Parameter
Description
other
Required. The collection to look for shared items in. It must not be null.
Return Value
Return Value
Description
bool
true 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);
}
}
Output
True
False
False
3
How It Works
Touching circles. The array and a both hold the number 3, so one shared item is enough for True.
No contact. 8 and 9 are not in a, so the answer is False.
Empty has nothing to share. An empty array cannot share any item, so the third line is False.
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));
}
}
Output
False
False
True
True
How It Works
Empty set. It has no item that could be shared, so empty.Overlaps(a) is False.
Empty with empty. Two empty sets share nothing either, so the answer is still False. Equal does not mean overlapping here.
Set with itself. Every item of a is in a, so a non-empty set always overlaps itself: True.
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);
}
}
Output
Slot A conflicts: True
Slot B conflicts: False
Busy hours: 3
How It Works
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.
Slot A. It needs hours 10 and 11. Hour 10 is busy, so the two collections share an item and Overlaps returns True.
Slot B. Hours 12 and 13 are both free, so nothing is shared and the answer is False.
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.