Capacity of a SortedList in C#

The Capacity property of a SortedList in C# represents the maximum number of key-value pairs that the SortedList can hold without needing to resize its internal storage. This is different from the Count property, which shows the actual number of elements currently stored.

When elements are added to a SortedList, the capacity automatically grows to accommodate new items. The capacity typically doubles when the current capacity is exceeded, following a geometric growth pattern to optimize performance.

Syntax

Following is the syntax to get the capacity of a SortedList −

int capacity = sortedList.Capacity;

Following is the syntax to set the capacity of a SortedList −

sortedList.Capacity = newCapacity;

Understanding Capacity Growth

SortedList Capacity vs Count Count = 3 Actual elements stored Capacity = 4 Maximum without resizing ? Add 2 more elements Capacity doubles to 8 (Count becomes 5) Capacity grows automatically: 4 ? 8 ? 16 ? 32... Clearing elements keeps the capacity unchanged

Example - Observing Capacity Growth

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args) {
        SortedList sortedList = new SortedList();
        sortedList.Add("A", "1");
        sortedList.Add("B", "2");
        sortedList.Add("C", "3");
        sortedList.Add("D", "4");
        sortedList.Add("E", "5");
        sortedList.Add("F", "6");
        sortedList.Add("G", "7");
        sortedList.Add("H", "8");
        sortedList.Add("I", "9");
        sortedList.Add("J", "10");
        Console.WriteLine("SortedList elements...");
        foreach(DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
        }
        Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
        Console.WriteLine("Capacity of SortedList = "+sortedList.Capacity);
        Console.WriteLine("\nEnumerator to iterate through the SortedList...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
    }
}

The output of the above code is −

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10
Count of SortedList key-value pairs = 10
Capacity of SortedList = 16

Enumerator to iterate through the SortedList...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10

Example - Capacity After Clearing Elements

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args) {
        SortedList sortedList = new SortedList();
        sortedList.Add("A", "1");
        sortedList.Add("B", "2");
        sortedList.Add("C", "3");
        sortedList.Add("D", "4");
        sortedList.Add("E", "5");
        sortedList.Add("F", "6");
        Console.WriteLine("SortedList elements...");
        foreach(DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
        }
        Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
        Console.WriteLine("Capacity of SortedList = "+sortedList.Capacity);
        sortedList.Clear();
        Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
        Console.WriteLine("Capacity of SortedList = "+sortedList.Capacity);
    }
}

The output of the above code is −

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Count of SortedList key-value pairs = 6
Capacity of SortedList = 16
Count of SortedList key-value pairs = 0
Capacity of SortedList = 16

Setting Initial Capacity

You can set an initial capacity when creating a SortedList to optimize performance if you know the approximate number of elements −

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args) {
        SortedList sortedList = new SortedList(50);
        sortedList.Add("X", "100");
        sortedList.Add("Y", "200");
        Console.WriteLine("Count = " + sortedList.Count);
        Console.WriteLine("Initial Capacity = " + sortedList.Capacity);
        sortedList.Capacity = 25;
        Console.WriteLine("Modified Capacity = " + sortedList.Capacity);
    }
}

The output of the above code is −

Count = 2
Initial Capacity = 50
Modified Capacity = 25

Conclusion

The Capacity property of a SortedList indicates the maximum number of elements it can hold without resizing. Unlike Count, capacity remains unchanged when elements are cleared and can be manually set to optimize memory usage and performance based on expected data size.

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

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements