Remove the entry at specified index from OrderedDictionary in C#

The OrderedDictionary class in C# provides the RemoveAt() method to remove an entry at a specified index. This method removes the key-value pair located at the given zero-based index position while maintaining the order of remaining elements.

Syntax

Following is the syntax for the RemoveAt() method −

public void RemoveAt(int index)

Parameters

  • index − The zero-based index of the entry to remove from the OrderedDictionary.

Using RemoveAt() Method

The following example demonstrates removing an entry at a specific index from an OrderedDictionary

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("A", "Books");
        dict.Add("B", "Electronics");
        dict.Add("C", "Smart Wearables");
        dict.Add("D", "Pet Supplies");
        dict.Add("E", "Clothing");
        dict.Add("F", "Footwear");
        
        Console.WriteLine("OrderedDictionary elements...");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
        
        dict.RemoveAt(4);
        Console.WriteLine("After removing element at index 4:");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of elements in OrderedDictionary (Updated) = " + dict.Count);
    }
}

The output of the above code is −

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
After removing element at index 4:
A Books
B Electronics
C Smart Wearables
D Pet Supplies
F Footwear
Count of elements in OrderedDictionary (Updated) = 5

Removing Multiple Entries by Index

You can remove multiple entries by calling RemoveAt() multiple times. Note that after each removal, the indices of subsequent elements shift down −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "AB");
        dict.Add("2", "CD");
        dict.Add("3", "MN");
        dict.Add("4", "PQ");
        
        Console.WriteLine("Original OrderedDictionary elements...");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count = " + dict.Count);
        
        dict.RemoveAt(1); // Removes "2 CD"
        dict.RemoveAt(2); // Now removes "4 PQ" (was at index 3, now at index 2)
        
        Console.WriteLine("\nAfter removing elements at indices 1 and 2:");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count = " + dict.Count);
    }
}

The output of the above code is −

Original OrderedDictionary elements...
1 AB
2 CD
3 MN
4 PQ
Count = 4

After removing elements at indices 1 and 2:
1 AB
3 MN
Count = 2

How It Works

When you call RemoveAt(index), the OrderedDictionary removes the key-value pair at the specified index and shifts all subsequent elements down by one position. This maintains the sequential order of the remaining elements while adjusting their indices accordingly.

Conclusion

The RemoveAt() method provides an efficient way to remove entries from an OrderedDictionary by index position. Remember that removing an element causes all subsequent elements to shift down, which affects their index positions for any further operations.

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

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements