Get an enumerator that iterates through the ListDictionary in C#

To get an enumerator that iterates through the ListDictionary, you can use the GetEnumerator() method, which returns an IDictionaryEnumerator. This allows you to iterate through the key-value pairs in the dictionary using a while loop or foreach loop.

The ListDictionary is a specialized collection class that implements a dictionary using a singly linked list, making it efficient for small collections (typically 10 or fewer elements).

Syntax

Following is the syntax for getting an enumerator from a ListDictionary −

IDictionaryEnumerator enumerator = listDictionary.GetEnumerator();
while (enumerator.MoveNext()) {
   // Access enumerator.Key and enumerator.Value
}

You can also use a foreach loop with DictionaryEntry

foreach (DictionaryEntry entry in listDictionary) {
   // Access entry.Key and entry.Value
}

Using GetEnumerator() with IDictionaryEnumerator

The GetEnumerator() method returns an IDictionaryEnumerator that provides access to both keys and values −

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

public class Demo {
   public static void Main() {
      ListDictionary dict = new ListDictionary();
      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("ListDictionary key-value pairs using GetEnumerator():");
      IDictionaryEnumerator demoEnum = dict.GetEnumerator();
      while (demoEnum.MoveNext()) {
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      }
   }
}

The output of the above code is −

ListDictionary key-value pairs using GetEnumerator():
Key = A, Value = Books
Key = B, Value = Electronics
Key = C, Value = Smart Wearables
Key = D, Value = Pet Supplies
Key = E, Value = Clothing
Key = F, Value = Footwear

Using foreach with DictionaryEntry

An alternative approach is to use a foreach loop with DictionaryEntry, which internally uses the enumerator −

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

public class Demo {
   public static void Main() {
      ListDictionary dict = new ListDictionary();
      dict.Add("1", "One");
      dict.Add("2", "Two");
      dict.Add("3", "Three");
      dict.Add("4", "Four");
      dict.Add("5", "Five");
      
      Console.WriteLine("ListDictionary elements using foreach:");
      foreach (DictionaryEntry entry in dict) {
         Console.WriteLine(entry.Key + " = " + entry.Value);
      }
   }
}

The output of the above code is −

ListDictionary elements using foreach:
1 = One
2 = Two
3 = Three
4 = Four
5 = Five

Comparison of Iteration Methods

Method Return Type Usage
GetEnumerator() IDictionaryEnumerator Manual iteration with while loop
foreach with DictionaryEntry DictionaryEntry Automatic iteration, cleaner syntax

Conclusion

The GetEnumerator() method provides an IDictionaryEnumerator for manually iterating through a ListDictionary. For simpler code, you can use a foreach loop with DictionaryEntry, which internally uses the same enumerator mechanism.

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

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements