Check if a SortedSet is a subset of the specified collection in C#

The SortedSet<T> class in C# provides the IsSubsetOf() method to check if all elements of one SortedSet are contained within another collection. A subset means that every element in the first set exists in the second set, though the second set may contain additional elements.

Syntax

Following is the syntax for the IsSubsetOf() method −

public bool IsSubsetOf(IEnumerable<T> other)

Parameters

  • other − The collection to compare with the current SortedSet.

Return Value

Returns true if the current SortedSet is a subset of the specified collection; otherwise, false.

Subset Relationship Set1 {AB, BC, CD} Set2 {AB, BC, CD, DE, EF} Set1 ? Set2 = True

Using IsSubsetOf() with Complete Subset

This example demonstrates checking if one SortedSet is a complete subset of another −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("AB");
        set1.Add("BC");
        set1.Add("CD");
        set1.Add("EF");
        
        Console.WriteLine("Elements in SortedSet1...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }
        
        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");
        
        Console.WriteLine("Elements in SortedSet2...");
        foreach (string res in set2) {
            Console.WriteLine(res);
        }
        
        Console.WriteLine("SortedSet1 is a subset of SortedSet2? = " + set1.IsSubsetOf(set2));
    }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet1 is a subset of SortedSet2? = True

Using IsSubsetOf() with Single Element

This example shows subset checking when the first set contains only one element −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("CD");
        set1.Add("CD"); // Duplicate - will be ignored
        set1.Add("CD");
        set1.Add("CD");
        
        Console.WriteLine("Elements in SortedSet1...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }
        
        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");
        
        Console.WriteLine("Elements in SortedSet2...");
        foreach (string res in set2) {
            Console.WriteLine(res);
        }
        
        Console.WriteLine("SortedSet1 is a subset of SortedSet2? = " + set1.IsSubsetOf(set2));
    }
}

The output of the above code is −

Elements in SortedSet1...
CD
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet1 is a subset of SortedSet2? = True

Using IsSubsetOf() with Non-Subset

This example demonstrates when a SortedSet is not a subset of another collection −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<int> numbers1 = new SortedSet<int>();
        numbers1.Add(1);
        numbers1.Add(2);
        numbers1.Add(5); // 5 is not in numbers2
        
        SortedSet<int> numbers2 = new SortedSet<int>();
        numbers2.Add(1);
        numbers2.Add(2);
        numbers2.Add(3);
        numbers2.Add(4);
        
        Console.WriteLine("Numbers1: {" + string.Join(", ", numbers1) + "}");
        Console.WriteLine("Numbers2: {" + string.Join(", ", numbers2) + "}");
        Console.WriteLine("Is Numbers1 subset of Numbers2? = " + numbers1.IsSubsetOf(numbers2));
    }
}

The output of the above code is −

Numbers1: {1, 2, 5}
Numbers2: {1, 2, 3, 4}
Is Numbers1 subset of Numbers2? = False

Key Points

  • The IsSubsetOf() method works with any IEnumerable<T> collection, not just SortedSets.

  • An empty SortedSet is considered a subset of any collection.

  • A SortedSet is always a subset of itself.

  • SortedSets automatically handle duplicates − adding the same element multiple times stores it only once.

Conclusion

The IsSubsetOf() method provides an efficient way to determine if all elements of a SortedSet exist within another collection. It returns true when every element in the current set can be found in the specified collection, making it useful for set theory operations and data validation scenarios.

Updated on: 2026-03-17T07:04:36+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements