Remove from the specified index of a SortedList in C#

The SortedList class in C# provides the RemoveAt() method to remove a key-value pair from a specified index. The RemoveAt() method removes the element at the specified zero-based index and automatically shifts the remaining elements to fill the gap.

Syntax

Following is the syntax for the RemoveAt() method −

public virtual void RemoveAt(int index)

Parameters

  • index − The zero-based index of the element to remove.

Key Points

  • The index is zero-based, meaning the first element is at index 0.

  • After removal, all elements at higher indices are shifted down by one position.

  • The Count property decreases by one after each removal.

  • An ArgumentOutOfRangeException is thrown if the index is negative or greater than or equal to Count.

RemoveAt(2) - Index-based Removal Before: A:1 idx 0 B:2 idx 1 C:3 idx 2 D:4 idx 3 Remove After: A:1 idx 0 B:2 idx 1 D:4 idx 2

Using RemoveAt() Method

Example 1 − Removing Single Element

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.RemoveAt(3);
      Console.WriteLine("\nEnumerator to iterate through the SortedList...");
      IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("Count of SortedList key-value pairs (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

Enumerator to iterate through the SortedList...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
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 (Updated) = 9

Example 2 − Removing Multiple Elements

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList sortedList = new SortedList();
      sortedList.Add("One", "Mouse");
      sortedList.Add("Two", "Keyboard");
      sortedList.Add("Three", "Headphone");
      sortedList.Add("Four", "Speakers");
      sortedList.Add("Five", "RAM");
      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.RemoveAt(1);
      sortedList.RemoveAt(2);
      Console.WriteLine("\nEnumerator to iterate through the SortedList...");
      IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
   }
}

The output of the above code is −

SortedList elements...
Key = Five, Value = RAM
Key = Four, Value = Speakers
Key = One, Value = Mouse
Key = Three, Value = Headphone
Key = Two, Value = Keyboard
Count of SortedList key-value pairs = 5

Enumerator to iterate through the SortedList...
Key = Five, Value = RAM
Key = One, Value = Mouse
Key = Two, Value = Keyboard
Count of SortedList key-value pairs (Updated) = 3

Conclusion

The RemoveAt() method provides an efficient way to remove elements from a SortedList by specifying their zero-based index. Remember that removing elements shifts the indices of subsequent elements, so be careful when removing multiple elements in a loop.

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

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements