SortedDictionary.Count Property in C#

The SortedDictionary.Count property in C# returns the number of key/value pairs contained in the SortedDictionary<TKey, TValue>. This read-only property is useful for determining the size of the collection and checking if it's empty.

Syntax

Following is the syntax for the Count property −

public int Count { get; }

Return Value

The Count property returns an int value representing the total number of key/value pairs in the SortedDictionary. It returns 0 if the dictionary is empty.

SortedDictionary Count Property Key-Value Pairs: {100: "Mobile"}, {200: "Laptop"} {300: "Desktop"}, {400: "Speakers"} Count = 4 Returns the total number of pairs

Using Count with Dictionary Operations

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      
      Console.WriteLine("SortedDictionary key-value pairs...");
      foreach(var pair in sortedDict) {
         Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
      }
      
      Console.WriteLine("Count of SortedDictionary key-value pairs = " + sortedDict.Count);
      Console.WriteLine("The SortedDictionary has the key 200? = " + sortedDict.ContainsKey(200));
      
      sortedDict.Clear();
      Console.WriteLine("Count after clearing = " + sortedDict.Count);
   }
}

The output of the above code is −

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
Count of SortedDictionary key-value pairs = 6
The SortedDictionary has the key 200? = True
Count after clearing = 0

Count Property with Dynamic Operations

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Inspiron");
      sortedDict.Add(200, "Alienware");
      sortedDict.Add(300, "Projectors");
      sortedDict.Add(400, "XPS");
      
      Console.WriteLine("Initial count: " + sortedDict.Count);
      
      sortedDict.Add(800, "Notebook");
      sortedDict.Add(10000, "Bluetooth Speaker");
      
      Console.WriteLine("Count after adding items: " + sortedDict.Count);
      
      sortedDict.Remove(300);
      Console.WriteLine("Count after removing key 300: " + sortedDict.Count);
      
      Console.WriteLine("\nFinal SortedDictionary contents:");
      foreach(var pair in sortedDict) {
         Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
      }
   }
}

The output of the above code is −

Initial count: 4
Count after adding items: 6
Count after removing key 300: 5

Final SortedDictionary contents:
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker

Common Use Cases

  • Checking if dictionary is empty: if (sortedDict.Count == 0)

  • Loop bounds: Use Count to determine iteration limits in custom loops

  • Memory management: Monitor collection size for performance optimization

  • Validation: Ensure minimum or maximum number of elements before processing

Conclusion

The SortedDictionary.Count property provides an efficient O(1) way to get the number of key/value pairs in the collection. It automatically updates as items are added or removed, making it essential for collection size monitoring and validation operations.

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

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements