In C#, HashSet.IsSupersetOf(other) asks: does my set contain every item of other? If yes, the answer is true. It is the mirror image of IsSubsetOf(): the other circle sits entirely inside yours.
This is the easy way to ask "do I have all of these?" Does the pantry have all the ingredients? Does the user have all the required rights? An equal collection counts, and an empty collection is always covered. Nothing is changed.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.IsSupersetOf(...). 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 IsSupersetOf on the big set and pass the collection of items you need.
C#
HashSet<int> big = new HashSet<int> { 1, 2, 3, 4 };
bool a = big.IsSupersetOf(new int[] { 1, 2 }); // true
bool b = big.IsSupersetOf(new int[] { 2, 5 }); // false: 5 is missing
bool c = big.IsSupersetOf(new int[0]); // true: nothing required
Call
What it does
big.IsSupersetOf(needed)
true when every item in needed is also in big.
big.IsSupersetOf(big)
true: a set contains all of its own items.
anySet.IsSupersetOf(new int[0])
true: an empty collection has no item that could be missing.
Passing null throws ArgumentNullException. Repeated values in other do not matter. big.IsSupersetOf(small) is the same question as small.IsSubsetOf(big).
Library declarations
C#
public bool IsSupersetOf(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection of items that must all be in the set. It must not be null.
Return Value
Return Value
Description
bool
true if the set contains every item of other (including when other is empty or equal). Otherwise false.
C# Examples
Example 1 — Does the Big Set Have Everything?
Ask whether a set of four numbers covers two different small lists.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> big = new HashSet<int> { 1, 2, 3, 4 };
Console.WriteLine(big.IsSupersetOf(new int[] { 1, 2 }));
Console.WriteLine(big.IsSupersetOf(new int[] { 2, 5 }));
Console.WriteLine(big.IsSupersetOf(big));
Console.WriteLine(big.Count);
}
}
Output
True
False
True
4
How It Works
Covered. Both 1 and 2 are in big, so the first call is True.
One is missing. The list {2, 5} has a 5, which big lacks. A single missing item makes the answer False.
Itself. A set contains all of its own items, so big.IsSupersetOf(big) is True.
Nothing changed. The checks only read, so Count is still 4.
Example 2 — Empty Collections and Repeated Values
See how an empty collection, an empty set, and a list full of repeats behave.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> empty = new HashSet<int>();
Console.WriteLine(a.IsSupersetOf(new int[0]));
Console.WriteLine(empty.IsSupersetOf(new int[0]));
Console.WriteLine(empty.IsSupersetOf(new int[] { 1 }));
Console.WriteLine(a.IsSupersetOf(new int[] { 1, 1, 1, 1 }));
}
}
Output
True
True
False
True
How It Works
Nothing required. An empty collection asks for no items, so every set covers it, and the first line is True.
Empty covers empty. Even an empty set is a superset of an empty collection, since there is nothing to be missing.
Empty cannot cover an item. The empty set does not hold 1, so the third line is False.
Repeats are harmless. The array lists 1 four times, but the question is still just "is 1 in the set?". The answer is True.
Example 3 — Check a Recipe Against the Pantry
A pantry set and two recipes. Use IsSupersetOf to test each one, then use ExceptWith on a copy to list what is missing.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> pantry = new HashSet<string> { "egg", "milk", "flour", "sugar" };
string[] pancakes = { "egg", "flour" };
string[] cake = { "egg", "butter", "sugar" };
Console.WriteLine("Pancakes: " + pantry.IsSupersetOf(pancakes));
Console.WriteLine("Cake: " + pantry.IsSupersetOf(cake));
HashSet<string> missing = new HashSet<string>(cake);
missing.ExceptWith(pantry);
Console.WriteLine("Missing: " + string.Join(", ", missing));
}
}
Output
Pancakes: True
Cake: False
Missing: butter
How It Works
Pantry versus recipe. The recipe is the small circle, and the pantry should be the big circle that holds all of it.
Pancakes pass."egg" and "flour" are both in the pantry, so IsSupersetOf returns True.
Cake fails."butter" is not in the pantry, so one missing item makes the answer False.
List what is missing. A copy of the cake list minus the pantry leaves only "butter". Only one item is left, so the print order cannot vary.
Frequently Asked Questions
It returns true if the set contains every item of the other collection, and false if at least one item is missing from the set.
Yes. IsSupersetOf returns true for an equal collection. Use IsProperSupersetOf to require at least one extra item.
true. An empty collection asks for no items, so there is nothing that could be missing.
Contains asks about one item. IsSupersetOf asks about a whole collection of items at once.
Copy the needed items into a new HashSet, then call ExceptWith with the set you are checking. What remains is the missing items.