Remove all elements from a SortedList in C#

To remove all elements from a SortedList in C#, you use the Clear() method. This method removes all key-value pairs from the SortedList and sets the Count property to zero.

Syntax

Following is the syntax for the Clear() method −

sortedList.Clear();

Parameters

The Clear() method does not take any parameters.

Return Value

The Clear() method does not return any value. It has a return type of void.

SortedList.Clear() Operation Before Clear() {A:1, B:2, C:3} Count = 3 Clear() After Clear() { } Count = 0

Using Clear() to Remove All Elements

Example

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);

      sortedList.Clear();
      Console.WriteLine("Count of SortedList (updated) = " + sortedList.Count);
   }
}

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
Count of SortedList (updated) = 0

Using Clear() on Generic SortedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      SortedList<string, int> sortedList = new SortedList<string, int>();
      sortedList.Add("Apple", 100);
      sortedList.Add("Banana", 200);
      sortedList.Add("Cherry", 300);

      Console.WriteLine("Count before Clear(): " + sortedList.Count);
      Console.WriteLine("SortedList elements...");
      foreach(KeyValuePair<string, int> kvp in sortedList) {
         Console.WriteLine("Key = " + kvp.Key + ", Value = " + kvp.Value);
      }

      sortedList.Clear();
      Console.WriteLine("Count after Clear(): " + sortedList.Count);
      Console.WriteLine("Is empty: " + (sortedList.Count == 0));
   }
}

The output of the above code is −

Count before Clear(): 3
SortedList elements...
Key = Apple, Value = 100
Key = Banana, Value = 200
Key = Cherry, Value = 300
Count after Clear(): 0
Is empty: True

Key Points

  • The Clear() method removes all elements in a single operation with O(1) time complexity.

  • After calling Clear(), the Count property returns 0.

  • The method works on both generic and non-generic versions of SortedList.

  • Memory allocated for the elements is released, making them eligible for garbage collection.

Conclusion

The Clear() method provides an efficient way to remove all key-value pairs from a SortedList in C#. It's a straightforward operation that sets the Count to zero and makes all stored elements eligible for garbage collection, ensuring optimal memory usage.

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

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements