In C#, HashSet.IsProperSupersetOf(other) is the strict version of IsSupersetOf(). It returns true only when your set contains every item of other and also has at least one item that other lacks.
With circles: the other circle is entirely inside yours, and yours is bigger. {1, 2, 3} is a proper superset of {1, 2}, but not of {1, 2, 3}, because there is no extra item. It never changes the set.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.IsProperSupersetOf(...). 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 IsProperSupersetOf on the bigger set and pass the smaller collection.
C#
HashSet<int> big = new HashSet<int> { 1, 2, 3 };
bool a = big.IsProperSupersetOf(new int[] { 1, 2 }); // true: has everything, plus 3
bool b = big.IsProperSupersetOf(new int[] { 1, 2, 3 }); // false: equal, nothing extra
bool c = big.IsSupersetOf(new int[] { 1, 2, 3 }); // true: equal is allowed here
Call
What it does
big.IsProperSupersetOf(small)
true when big holds all of small and at least one more item.
big.IsProperSupersetOf(equalSet)
false, because there is no extra item.
nonEmpty.IsProperSupersetOf(new int[0])
true: any non-empty set is a proper superset of an empty collection.
Passing null throws ArgumentNullException. An empty set is never a proper superset of anything. a.IsProperSupersetOf(b) is the same question as b.IsProperSubsetOf(a).
Library declarations
C#
public bool IsProperSupersetOf(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection that must be fully inside the set. It must not be null.
Return Value
Return Value
Description
bool
true if the set contains every item of other and also has at least one item that other does not have. Otherwise false.
C# Examples
Example 1 — Compare Proper and Plain Superset
Test a three-number set against a smaller set and against an equal set, with both superset methods.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> big = new HashSet<int> { 1, 2, 3 };
Console.WriteLine(big.IsProperSupersetOf(new int[] { 1, 2 }));
Console.WriteLine(big.IsProperSupersetOf(new int[] { 3, 2, 1 }));
Console.WriteLine(big.IsSupersetOf(new int[] { 3, 2, 1 }));
Console.WriteLine(big.IsProperSupersetOf(new int[] { 1, 9 }));
}
}
Output
True
False
True
False
How It Works
Bigger by one.big has 1 and 2, plus the extra 3, so the first call is True.
Same items. The array holds 3, 2, 1, which is the same set in another order. Nothing extra means IsProperSupersetOf is False.
Plain superset allows equal. For the same array IsSupersetOf says True. This pair of lines shows the meaning of "proper".
Missing item. The array has a 9 that big lacks, so big does not contain everything and both superset questions would be False.
Example 2 — Empty Sets and Proper Superset
Check an empty collection against a one-item set, and the empty set against everything.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> one = new HashSet<int> { 1 };
HashSet<int> empty = new HashSet<int>();
Console.WriteLine(one.IsProperSupersetOf(new int[0]));
Console.WriteLine(empty.IsProperSupersetOf(new int[0]));
Console.WriteLine(empty.IsProperSupersetOf(new int[] { 1 }));
Console.WriteLine(empty.IsSupersetOf(new int[0]));
}
}
Output
True
False
False
True
How It Works
Something versus nothing.one holds an item and the empty array holds none, so one has all of the (zero) items plus an extra one: True.
Empty versus empty. They are equal, so there is no extra item. The answer is False.
Empty cannot be bigger. The empty set cannot contain the item 1, so it is not a superset of {1} at all.
Plain superset differs. For the equal pair, IsSupersetOf on the last line is True, because equal is allowed there.
Example 3 — Detect That a Set Has Grown
Take a snapshot copy, change the set, and use IsProperSupersetOf to see whether the set only gained items.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> scores = new HashSet<int> { 10, 20 };
HashSet<int> snapshot = new HashSet<int>(scores);
Console.WriteLine(scores.IsProperSupersetOf(snapshot));
scores.Add(30);
Console.WriteLine(scores.IsProperSupersetOf(snapshot));
scores.Remove(10);
Console.WriteLine(scores.IsProperSupersetOf(snapshot));
Console.WriteLine(scores.Overlaps(snapshot));
}
}
Output
False
True
False
True
How It Works
Snapshot.new HashSet<int>(scores) copies the set at one moment. Right now both are equal, so the first answer is False.
After Add.scores now has 10, 20, and 30. It holds everything in the snapshot plus 30, so the answer is True.
After Remove. Taking away 10 breaks the "holds everything" rule, so scores is no longer a proper superset, and the answer is False.
Still related. The sets still share 20, so Overlaps in the last line is True. A set can overlap another without being its superset.
Frequently Asked Questions
IsSupersetOf is true when the set contains every item of the other collection, even if they are equal. IsProperSupersetOf also needs the set to have at least one extra item.
false. Equal sets have no extra item, so neither is a proper superset of the other.
No. It has no items, so it can never have an extra one. IsProperSupersetOf on an empty set is always false.
Yes. It holds all of the zero items in the collection, and it has at least one extra item.
They ask the same question from two sides: a.IsProperSupersetOf(b) is true exactly when b.IsProperSubsetOf(a) is true.