Get an IDictionaryEnumerator object in OrderedDictionary in C#

The OrderedDictionary class in C# provides the GetEnumerator() method to obtain an IDictionaryEnumerator object. This enumerator allows you to iterate through the key-value pairs while maintaining the insertion order of elements.

The IDictionaryEnumerator is specifically designed for dictionary collections and provides access to both the Key and Value properties of each element during enumeration.

Syntax

Following is the syntax for getting an IDictionaryEnumerator from an OrderedDictionary

IDictionaryEnumerator enumerator = orderedDictionary.GetEnumerator();

Following is the syntax for iterating through the enumerator −

while (enumerator.MoveNext()) {
    Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value);
}

Return Value

The GetEnumerator()IDictionaryEnumerator object that provides the following properties −

  • Key - Gets the key of the current element

  • Value - Gets the value of the current element

  • Current - Gets the current element as a DictionaryEntry

OrderedDictionary Enumeration Process OrderedDictionary Key1: Value1 Key2: Value2 GetEnumerator() IDictionaryEnumerator Key, Value, Current MoveNext() Maintains insertion order during enumeration

Using IDictionaryEnumerator with Numbers

Example

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

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");
        dict.Add("7", "Seven");
        dict.Add("8", "Eight");
        
        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

The output of the above code is −

Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Key = 7, Value = Seven
Key = 8, Value = Eight

Using IDictionaryEnumerator with Categories

Example

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

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "Appliances");
        dict.Add("2", "Supplies");
        dict.Add("3", "Electronics");
        dict.Add("4", "Clothing");
        dict.Add("5", "Books");
        dict.Add("6", "Accessories");
        
        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

The output of the above code is −

Key = 1, Value = Appliances
Key = 2, Value = Supplies
Key = 3, Value = Electronics
Key = 4, Value = Clothing
Key = 5, Value = Books
Key = 6, Value = Accessories

How It Works

The enumeration process follows these steps −

  • GetEnumerator() - Returns an IDictionaryEnumerator positioned before the first element

  • MoveNext() - Advances the enumerator to the next element and returns true if successful

  • Key and Value - Properties that provide access to the current element's key and value

  • Insertion Order - Elements are enumerated in the same order they were added

Conclusion

The GetEnumerator() method of OrderedDictionary returns an IDictionaryEnumerator that preserves the insertion order during iteration. This enumerator provides convenient access to both keys and values through its Key and Value properties, making it ideal for ordered dictionary traversal.

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

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements