Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a SortedSet object is a proper subset of the specified collection in C#
A proper subset is a set where all elements of one set are contained in another set, but the two sets are not equal. In C#, the SortedSet<T> class provides the IsProperSubsetOf() method to check if a SortedSet is a proper subset of a specified collection.
Syntax
Following is the syntax for the IsProperSubsetOf() method −
public bool IsProperSubsetOf(IEnumerable<T> other)
Parameters
- other − The collection to compare with the current SortedSet.
Return Value
Returns true if the SortedSet is a proper subset of the specified collection; otherwise, false.
Using IsProperSubsetOf() with True Result
In this example, set2 contains fewer elements than set1, and all elements of set2 exist in set1 −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(20);
set1.Add(40);
set1.Add(60);
set1.Add(80);
set1.Add(100);
set1.Add(120);
set1.Add(140);
Console.WriteLine("Elements in SortedSet1...");
foreach (int res in set1){
Console.WriteLine(res);
}
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(20);
set2.Add(40);
set2.Add(60);
Console.WriteLine("Elements in SortedSet2...");
foreach (int res in set2){
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 is a proper subset of SortedSet1? = "+set2.IsProperSubsetOf(set1));
}
}
The output of the above code is −
Elements in SortedSet1... 20 40 60 80 100 120 140 Elements in SortedSet2... 20 40 60 SortedSet2 is a proper subset of SortedSet1? = True
Using IsProperSubsetOf() with False Result
In this example, both sets have identical elements, making them equal rather than one being a proper subset of the other −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(20);
set1.Add(40);
set1.Add(60);
Console.WriteLine("Elements in SortedSet1...");
foreach (int res in set1){
Console.WriteLine(res);
}
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(20);
set2.Add(40);
set2.Add(60);
Console.WriteLine("Elements in SortedSet2...");
foreach (int res in set2){
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 is a proper subset of SortedSet1? = "+set2.IsProperSubsetOf(set1));
}
}
The output of the above code is −
Elements in SortedSet1... 20 40 60 Elements in SortedSet2... 20 40 60 SortedSet2 is a proper subset of SortedSet1? = False
Key Rules
-
A proper subset must contain fewer elements than the superset.
-
All elements in the subset must exist in the superset.
-
If two sets are equal (same elements), one is not a proper subset of the other.
-
An empty set is a proper subset of any non-empty set.
Conclusion
The IsProperSubsetOf() method in C# determines if a SortedSet is a proper subset of another collection. It returns true only when all elements of the calling set exist in the target collection, and the calling set has fewer elements than the target collection.
