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

The IsProperSubsetOf() method in C# determines whether a HashSet is a proper subset of a specified collection. A proper subset means all elements of the first set exist in the second set, but the second set contains additional elements that are not in the first set.

Syntax

Following is the syntax for the IsProperSubsetOf() method −

public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

  • other − The collection to compare to the current HashSet

Return Value

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

Proper Subset Relationship 1 2 3 4 5 6 7 Set A Set B A is a proper subset of B: all elements of A are in B, but B has more elements

Using IsProperSubsetOf() with True Result

This example demonstrates when one HashSet is a proper subset of another −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        HashSet<int> set1 = new HashSet<int>();
        set1.Add(70);
        set1.Add(100);
        set1.Add(125);
        set1.Add(150);
        Console.WriteLine("Elements in HashSet1");
        foreach(int val in set1) {
            Console.WriteLine(val);
        }
        
        HashSet<int> set2 = new HashSet<int>();
        set2.Add(30);
        set2.Add(60);
        set2.Add(70);
        set2.Add(80);
        set2.Add(100);
        set2.Add(125);
        set2.Add(150);
        set2.Add(200);
        Console.WriteLine("Elements in HashSet2");
        foreach(int val in set2) {
            Console.WriteLine(val);
        }
        
        Console.WriteLine("Is set1 a proper subset of set2? " + set1.IsProperSubsetOf(set2));
    }
}

The output of the above code is −

Elements in HashSet1
70
100
125
150
Elements in HashSet2
30
60
70
80
100
125
150
200
Is set1 a proper subset of set2? True

Using IsProperSubsetOf() with False Result

This example shows when a HashSet is not a proper subset because the sets have no common elements −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        HashSet<int> set1 = new HashSet<int>();
        set1.Add(10);
        set1.Add(20);
        Console.WriteLine("Elements in HashSet1");
        foreach(int val in set1) {
            Console.WriteLine(val);
        }
        
        HashSet<int> set2 = new HashSet<int>();
        set2.Add(30);
        set2.Add(60);
        set2.Add(70);
        set2.Add(80);
        set2.Add(100);
        set2.Add(125);
        set2.Add(150);
        set2.Add(200);
        Console.WriteLine("Elements in HashSet2");
        foreach(int val in set2) {
            Console.WriteLine(val);
        }
        
        Console.WriteLine("Is set1 a proper subset of set2? " + set1.IsProperSubsetOf(set2));
    }
}

The output of the above code is −

Elements in HashSet1
10
20
Elements in HashSet2
30
60
70
80
100
125
150
200
Is set1 a proper subset of set2? False

Key Rules for Proper Subsets

  • All elements of the first set must exist in the second set
  • The second set must contain at least one additional element not in the first set
  • An empty HashSet is a proper subset of any non-empty HashSet
  • A HashSet is never a proper subset of itself

Conclusion

The IsProperSubsetOf() method checks if all elements of a HashSet exist in another collection while ensuring the other collection has additional elements. This method is useful for validating hierarchical relationships between data sets and implementing set-based logic in applications.

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

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements