C# HashSet IsSubsetOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In C#, HashSet.IsSubsetOf(other) asks: is every item of my set also inside other? If yes, the answer is true. In Venn diagram words, your whole circle sits inside the other circle (it may fill it completely).

For example, {1, 2} is a subset of {1, 2, 3, 4}. A set is also a subset of an equal set, and the empty set is a subset of everything. If you need "inside, but not equal", use IsProperSubsetOf(). This 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.IsSubsetOf(...). 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 IsSubsetOf on the small set and pass the larger collection. The argument can be any IEnumerable<T>.

C#
HashSet<int> small = new HashSet<int> { 1, 2 };
HashSet<int> big = new HashSet<int> { 1, 2, 3, 4 };

bool a = small.IsSubsetOf(big);   // true
bool b = big.IsSubsetOf(small);   // false: 3 and 4 are missing
CallWhat it does
small.IsSubsetOf(big)true when every item of small is also in big.
small.IsSubsetOf(new int[] { 2, 1 })Arrays and lists work too. Order and repeats in the argument do not matter.
empty.IsSubsetOf(anything)Always true: an empty set has no item that could be missing.

Passing null throws ArgumentNullException. Equality of items uses the set's comparer. The direction matters: A.IsSubsetOf(B) and B.IsSubsetOf(A) are different questions.

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

Parameters

ParameterDescription
otherRequired. The collection to compare with. It must not be null.

Return Value

Return ValueDescription
booltrue if every item of the set is also in other (including when the two are equal, or when the set is empty). Otherwise false.

C# Examples

Example 1 — Is the Small Set Inside the Big One?

Ask the question in both directions, and with a set that has an item the big set does not.

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

class Program {
    static void Main() {
        HashSet<int> small = new HashSet<int> { 1, 2 };
        HashSet<int> big = new HashSet<int> { 1, 2, 3, 4 };
        HashSet<int> stranger = new HashSet<int> { 1, 5 };

        Console.WriteLine(small.IsSubsetOf(big));
        Console.WriteLine(big.IsSubsetOf(small));
        Console.WriteLine(stranger.IsSubsetOf(big));
        Console.WriteLine(small.Count);
    }
}

How It Works

  1. Small is inside. Both 1 and 2 appear in big, so small.IsSubsetOf(big) is True.
  2. Direction matters. big has 3 and 4, which are not in small, so the reverse question is False.
  3. One stranger is enough. stranger has the number 5, which big lacks. A single missing item makes the answer False.
  4. Read-only. The check changes nothing, so small.Count is still 2.

Example 2 — Equal Sets, Arrays, and the Empty Set

See that an equal collection still counts as a match, that the other side may be an array, and that the empty set fits inside everything.

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

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

        Console.WriteLine(a.IsSubsetOf(new int[] { 3, 2, 1 }));
        Console.WriteLine(a.IsSubsetOf(new int[] { 1, 2 }));

        HashSet<int> empty = new HashSet<int>();
        Console.WriteLine(empty.IsSubsetOf(a));
        Console.WriteLine(empty.IsSubsetOf(new int[0]));
        Console.WriteLine(a.IsSubsetOf(empty));
    }
}

How It Works

  1. Equal counts. The array holds exactly the same three numbers in another order. Order does not matter for sets, so the answer is True.
  2. Too small. The array {1, 2} lacks the 3, so a does not fit inside it.
  3. Empty fits anywhere. The empty set has no item that could be missing, so both checks with empty on the left are True, even against an empty array.
  4. Reverse is different. a has items, and empty has none to hold them, so a.IsSubsetOf(empty) is False.

Example 3 — Check Requested Permissions

A user has a set of granted permissions. A request is allowed only when every requested permission is already granted.

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

class Program {
    static void Main() {
        HashSet<string> granted = new HashSet<string> { "read", "write", "delete" };

        HashSet<string> request1 = new HashSet<string> { "read", "write" };
        HashSet<string> request2 = new HashSet<string> { "read", "admin" };

        if (request1.IsSubsetOf(granted)) {
            Console.WriteLine("Request 1: allowed");
        } else {
            Console.WriteLine("Request 1: denied");
        }

        if (request2.IsSubsetOf(granted)) {
            Console.WriteLine("Request 2: allowed");
        } else {
            Console.WriteLine("Request 2: denied");
        }
    }
}

How It Works

  1. Granted is the big circle. granted lists all the rights this user has. Each right is stored once.
  2. Each request is a small circle. A request is allowed only if it sits fully inside granted.
  3. First request. "read" and "write" are both granted, so IsSubsetOf is true and the first message is printed.
  4. Second request. "admin" was never granted. One missing right fails the whole check, so "Request 2: denied" is printed.

Frequently Asked Questions

It returns true if every item of the set is also in the other collection, and false if at least one item is missing there.
Yes. An equal collection counts as a subset, so IsSubsetOf returns true. Use IsProperSubsetOf when you want to rule that out.
Yes. IsSubsetOf returns true for an empty set, even when the other collection is empty too.
No. It only reads both and returns a bool.
IsSupersetOf. a.IsSubsetOf(b) asks whether a fits inside b, and b.IsSupersetOf(a) asks the same thing from the other side.

Next: IsSupersetOf()

Check whether the HashSet contains every item of another collection; returns true or false.

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

8 people found this page helpful