How to create a SortedSet in C#?

A SortedSet in C# is a collection that maintains its elements in sorted order automatically. It belongs to the System.Collections.Generic namespace and ensures that duplicate elements are not allowed while keeping all elements sorted based on their natural ordering or a custom comparer.

Syntax

Following is the syntax for creating a SortedSet −

SortedSet<T> setName = new SortedSet<T>();

You can also initialize with an existing collection −

SortedSet<T> setName = new SortedSet<T>(existingCollection);

To use a custom comparer −

SortedSet<T> setName = new SortedSet<T>(comparer);

Using SortedSet with Different Data Types

Example with String Elements

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

Example with Integer Elements and RemoveWhere

using System;
using System.Collections.Generic;

public class Demo {
   private static bool demo(int i){
      return ((i % 10) == 0);
   }
   
   public static void Main(String[] args){
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(200);
      set1.Add(215);
      set1.Add(310);
      set1.Add(500);
      set1.Add(600);
      Console.WriteLine("SortedSet elements...");
      foreach (int i in set1){
         Console.WriteLine(i);
      }
      Console.WriteLine(" ");
      
      set1.RemoveWhere(demo);
      Console.WriteLine("SortedSet after removing some elements...");
      foreach (int i in set1){
         Console.WriteLine(i);
      }
   }
}

The output of the above code is −

SortedSet elements...
200
215
310
500
600
 
SortedSet after removing some elements...
215

Creating SortedSet from Existing Collection

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      List<int> numbers = new List<int> { 50, 10, 30, 20, 40, 10, 30 };
      SortedSet<int> sortedNumbers = new SortedSet<int>(numbers);
      
      Console.WriteLine("Original List:");
      foreach (int num in numbers){
         Console.Write(num + " ");
      }
      
      Console.WriteLine("\nSortedSet (duplicates removed, sorted):");
      foreach (int num in sortedNumbers){
         Console.Write(num + " ");
      }
      
      Console.WriteLine("\nCount in List: " + numbers.Count);
      Console.WriteLine("Count in SortedSet: " + sortedNumbers.Count);
   }
}

The output of the above code is −

Original List:
50 10 30 20 40 10 30 
SortedSet (duplicates removed, sorted):
10 20 30 40 50 
Count in List: 7
Count in SortedSet: 5

Key Features

Feature Description
Automatic Sorting Elements are automatically sorted upon insertion
No Duplicates Duplicate elements are not allowed
Fast Operations O(log n) time complexity for Add, Remove, Contains
Set Operations Supports Union, Intersection, and other set operations

Conclusion

SortedSet in C# provides an efficient way to store unique elements in sorted order automatically. It's ideal when you need a collection that maintains order, eliminates duplicates, and provides fast lookup operations. The collection automatically handles sorting and offers various methods for set operations and conditional removal of elements.

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

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements