Get the number of elements in the SortedSet in C#

The Count property in C# returns the number of elements currently stored in a SortedSet. This property provides an efficient way to determine the size of the collection without having to iterate through all elements.

Syntax

Following is the syntax for getting the count of elements in a SortedSet −

int elementCount = sortedSet.Count;

Return Value

The Count property returns an int value representing the total number of elements in the SortedSet. For an empty SortedSet, it returns 0.

Using Count Property with String SortedSet

The following example demonstrates how to get the count of elements in SortedSet collections containing strings −

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);
      }
      Console.WriteLine("Count of elements in SortedSet1 = " + set1.Count);
      
      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);
      }
      Console.WriteLine("Count of elements in SortedSet2 = " + set2.Count);
   }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Count of elements in SortedSet1 = 4
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK
Count of elements in SortedSet2 = 7

Using Count Property with Integer SortedSet

This example shows how the Count property changes when elements are added or removed from a SortedSet −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(100);
      set1.Add(200);
      set1.Add(300);
      set1.Add(400);
      
      Console.WriteLine("Elements in SortedSet...");
      foreach (int res in set1){
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in SortedSet = " + set1.Count);
      
      set1.Clear();
      Console.WriteLine("Count of elements in SortedSet (updated) = " + set1.Count);
   }
}

The output of the above code is −

Elements in SortedSet...
100
200
300
400
Count of elements in SortedSet = 4
Count of elements in SortedSet (updated) = 0

Key Features of Count Property

  • The Count property has O(1) time complexity − it returns the count instantly without iterating through elements.

  • Automatic updates − The count is automatically maintained when elements are added or removed.

  • Read-only property − You cannot directly set the count value; it changes only through add/remove operations.

  • Handles duplicates − Since SortedSet doesn't allow duplicates, attempting to add duplicate elements won't increase the count.

Conclusion

The Count property provides an efficient way to determine the number of elements in a SortedSet with O(1) complexity. It automatically updates when elements are added or removed, making it reliable for checking collection size in real-time scenarios.

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

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements