SortedSet Class in C#

The SortedSet class in C# represents a collection of objects that is maintained in sorted order. Unlike regular sets, SortedSet automatically keeps elements sorted and ensures uniqueness − duplicate elements are not allowed.

SortedSet is part of the System.Collections.Generic namespace and provides efficient operations for adding, removing, and searching elements while maintaining the sorted order.

Syntax

Following is the syntax for creating and using a SortedSet −

SortedSet<T> setName = new SortedSet<T>();
setName.Add(element);

Key Properties

Property Description
Comparer Gets the IComparer<T> object that is used to order the values in the SortedSet<T>.
Count Gets the number of elements in the SortedSet<T>.
Max Gets the maximum value in the SortedSet<T>, as defined by the comparer.
Min Gets the minimum value in the SortedSet<T>, as defined by the comparer.

Common Methods

Method Description
Add(T) Adds an element to the set and returns a value that indicates if it was successfully added.
Clear() Removes all elements from the set.
Contains(T) Determines whether the set contains a specific element.
Remove(T) Removes the specified element from the set.
CopyTo(T[]) Copies the complete SortedSet<T> to a compatible one-dimensional array.

SortedSet Characteristics Automatic Sorting Elements kept in sorted order automatically No Duplicates Each element appears only once Fast Search O(log n) operations

Using SortedSet with Contains and Set Operations

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("CD"); // Duplicate - will not be added
      set1.Add("CD");
      set1.Add("CD");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet1 contains the element DE? = " + set1.Contains("DE"));
      
      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
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True

Using Enumerator with SortedSet

Example

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 (Enumerator for SortedSet)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK

Using Min, Max, and Count Properties

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedSet<int> numbers = new SortedSet<int>();
      numbers.Add(45);
      numbers.Add(12);
      numbers.Add(78);
      numbers.Add(33);
      numbers.Add(89);
      numbers.Add(12); // Duplicate - will not be added
      
      Console.WriteLine("Numbers in SortedSet:");
      foreach (int num in numbers) {
         Console.WriteLine(num);
      }
      Console.WriteLine("Count: " + numbers.Count);
      Console.WriteLine("Min: " + numbers.Min);
      Console.WriteLine("Max: " + numbers.Max);
   }
}

The output of the above code is −

Numbers in SortedSet:
12
33
45
78
89
Count: 5
Min: 12
Max: 89

Conclusion

SortedSet in C# provides an efficient way to maintain a collection of unique elements in sorted order. It offers fast search operations and useful properties like Min and Max, making it ideal for scenarios where you need both uniqueness and automatic sorting of elements.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements