In C#, HashSet.IsProperSubsetOf(other) is a strict version of IsSubsetOf(). It returns true only when every item of your set is in otherandother has at least one extra item.
Say it with circles: your circle is inside the other circle and the other circle is bigger. {1, 2} is a proper subset of {1, 2, 3}, but it is not a proper subset of {1, 2}, because there is nothing extra. The method does not change the set.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.IsProperSubsetOf(...). 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 IsProperSubsetOf on the smaller set and pass the larger collection.
C#
HashSet<int> small = new HashSet<int> { 1, 2 };
bool a = small.IsProperSubsetOf(new int[] { 1, 2, 3 }); // true: inside, with an extra item
bool b = small.IsProperSubsetOf(new int[] { 1, 2 }); // false: equal, nothing extra
bool c = small.IsSubsetOf(new int[] { 1, 2 }); // true: equal is allowed here
Call
What it does
small.IsProperSubsetOf(big)
true when small is inside big and big has more.
small.IsProperSubsetOf(equalSet)
false, because an equal collection has nothing extra.
empty.IsProperSubsetOf(nonEmpty)
true: the empty set fits in any collection that has at least one item.
Passing null throws ArgumentNullException. The empty set is a proper subset of every non-empty collection, but not of an empty one. Repeats in other are ignored when the method decides what is "extra".
Library declarations
C#
public bool IsProperSubsetOf(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection to compare with. It must not be null.
Return Value
Return Value
Description
bool
true if the set is inside other and other has at least one item the set does not have. Otherwise false.
C# Examples
Example 1 — Compare Proper and Plain Subset
Check a small set against a bigger one and against an equal one, using both IsProperSubsetOf and IsSubsetOf.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> small = new HashSet<int> { 1, 2 };
HashSet<int> bigger = new HashSet<int> { 1, 2, 3 };
HashSet<int> same = new HashSet<int> { 2, 1 };
Console.WriteLine(small.IsProperSubsetOf(bigger));
Console.WriteLine(small.IsProperSubsetOf(same));
Console.WriteLine(small.IsSubsetOf(same));
}
}
Output
True
False
True
How It Works
Inside and smaller.bigger has the extra item 3, so small is a proper subset and the first line is True.
Equal is not proper.same holds exactly 1 and 2 (order does not matter). Nothing extra means IsProperSubsetOf is False.
Plain subset allows equal. The same pair of sets gives True with IsSubsetOf.
The one difference. The two methods only disagree when the sets are equal. That is the whole meaning of "proper".
Example 2 — The Empty Set and Proper Subset
Test the empty set against a collection with items, against another empty one, and in the opposite direction.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> empty = new HashSet<int>();
HashSet<int> one = new HashSet<int> { 1 };
Console.WriteLine(empty.IsProperSubsetOf(one));
Console.WriteLine(empty.IsProperSubsetOf(new int[0]));
Console.WriteLine(one.IsProperSubsetOf(empty));
Console.WriteLine(empty.IsSubsetOf(new int[0]));
}
}
Output
True
False
False
True
How It Works
Empty inside non-empty. The empty set has nothing that could be missing, and one has an extra item (1), so the first line is True.
Empty vs empty. Both are equal, so there is nothing extra and the answer is False. Plain IsSubsetOf in the last line says True for the same pair.
Wrong direction.one has the item 1, but empty cannot hold it, so one.IsProperSubsetOf(empty) is False.
Rule to remember. An empty set is a proper subset of every collection that has at least one item, and of nothing else.
Example 3 — Does the Senior Know Strictly More?
Compare skill sets: a junior set against a senior set that has one more skill, and against a peer with the same skills.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> junior = new HashSet<string> { "c#", "sql" };
HashSet<string> senior = new HashSet<string> { "c#", "sql", "azure" };
HashSet<string> peer = new HashSet<string> { "sql", "c#" };
Console.WriteLine(junior.IsProperSubsetOf(senior));
Console.WriteLine(junior.IsProperSubsetOf(peer));
Console.WriteLine(junior.SetEquals(peer));
}
}
Output
True
False
True
How It Works
Skills as a set. Each person has a set of skill names. Order does not matter, only which skills are present.
Senior knows more.junior is fully inside senior, and "azure" is extra, so IsProperSubsetOf returns True.
Peer knows the same.peer was written in a different order but holds the same two skills, so there is nothing extra and the answer is False.
Equality check.SetEquals confirms the two sets are the same, which is exactly why the proper check failed.
Frequently Asked Questions
IsSubsetOf is true when every item of the set is in the other collection, even if they are equal. IsProperSubsetOf also needs the other collection to have at least one extra item.
Yes, of any collection that has at least one item. It is not a proper subset of an empty collection.
false. Equal sets have nothing extra, so neither is a proper subset of the other.
No. A HashSet has no order, and the order of items in the other collection does not matter either.
IsProperSupersetOf. a.IsProperSubsetOf(b) is true exactly when b.IsProperSupersetOf(a) is true.