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

To check if a SortedSet is a superset of the specified collection in C#, we use the IsSupersetOf() method. A superset means that one set contains all the elements of another set, and possibly additional elements.

Syntax

Following is the syntax for the IsSupersetOf() method −

public bool IsSupersetOf(IEnumerable<T> other)

Parameters

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

Return Value

Returns true if the SortedSet contains all elements of the specified collection; otherwise, false.

Superset Relationship Set2 (Superset) Set1 Set2 contains all elements of Set1

Using IsSupersetOf() with String Collections

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      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("SortedSet2 is a superset of SortedSet1? = " + set2.IsSupersetOf(set1));
   }
}

The output of the above code is −

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

Using IsSupersetOf() with Integer Collections

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(10);
      set1.Add(20);
      set1.Add(30);
      
      Console.WriteLine("Elements in SortedSet1...");
      foreach (int res in set1){
         Console.WriteLine(res);
      }
      
      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(10);
      set2.Add(20);
      set2.Add(30);
      set2.Add(40);
      set2.Add(50);
      set2.Add(60);
      
      Console.WriteLine("Elements in SortedSet2...");
      foreach (int res in set2){
         Console.WriteLine(res);
      }
      
      Console.WriteLine("SortedSet2 is a superset of SortedSet1? = " + set2.IsSupersetOf(set1));
   }
}

The output of the above code is −

Elements in SortedSet1...
10
20
30
Elements in SortedSet2...
10
20
30
40
50
60
SortedSet2 is a superset of SortedSet1? = True

Using IsSupersetOf() with Different Collections

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> sortedSet = new SortedSet<int>();
      sortedSet.Add(1);
      sortedSet.Add(2);
      sortedSet.Add(3);
      sortedSet.Add(4);
      sortedSet.Add(5);
      
      List<int> list = new List<int> { 2, 4 };
      int[] array = { 1, 3, 5 };
      HashSet<int> hashSet = new HashSet<int> { 1, 2, 6 };
      
      Console.WriteLine("SortedSet contains: 1, 2, 3, 4, 5");
      Console.WriteLine("List contains: 2, 4");
      Console.WriteLine("Array contains: 1, 3, 5");
      Console.WriteLine("HashSet contains: 1, 2, 6");
      Console.WriteLine();
      
      Console.WriteLine("Is SortedSet superset of List? " + sortedSet.IsSupersetOf(list));
      Console.WriteLine("Is SortedSet superset of Array? " + sortedSet.IsSupersetOf(array));
      Console.WriteLine("Is SortedSet superset of HashSet? " + sortedSet.IsSupersetOf(hashSet));
   }
}

The output of the above code is −

SortedSet contains: 1, 2, 3, 4, 5
List contains: 2, 4
Array contains: 1, 3, 5
HashSet contains: 1, 2, 6

Is SortedSet superset of List? True
Is SortedSet superset of Array? True
Is SortedSet superset of HashSet? False

Conclusion

The IsSupersetOf() method in C# checks if a SortedSet contains all elements of another collection. It returns true when the SortedSet is a superset, meaning it contains all elements from the specified collection, and false otherwise.

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

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements