Get an enumerator that iterates through the SortedDictionary in C#

The SortedDictionary<TKey, TValue> class in C# provides the GetEnumerator() method to iterate through its key-value pairs. This method returns an IDictionaryEnumerator that allows you to traverse the collection in ascending order of keys.

Syntax

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

IDictionaryEnumerator enumerator = sortedDictionary.GetEnumerator();

To iterate through the enumerator −

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

Return Value

The GetEnumerator() method returns an IDictionaryEnumerator object that provides access to the key-value pairs through the Key and Value properties. The enumerator maintains the sorted order of keys.

Using GetEnumerator() with String Values

Example

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

The output of the above code is −

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

Using GetEnumerator() with Integer Values

Example

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, int> sortedDict = new SortedDictionary<int, int>();
      sortedDict.Add(100, 1);
      sortedDict.Add(200, 2);
      sortedDict.Add(300, 3);
      sortedDict.Add(400, 4);
      sortedDict.Add(500, 5);
      sortedDict.Add(600, 6);
      sortedDict.Add(700, 7);
      sortedDict.Add(800, 8);
      sortedDict.Add(900, 9);
      sortedDict.Add(1000, 10);
      Console.WriteLine("SortedDictionary key-value pairs...");
      IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

The output of the above code is −

SortedDictionary key-value pairs...
Key = 100, Value = 1
Key = 200, Value = 2
Key = 300, Value = 3
Key = 400, Value = 4
Key = 500, Value = 5
Key = 600, Value = 6
Key = 700, Value = 7
Key = 800, Value = 8
Key = 900, Value = 9
Key = 1000, Value = 10

Alternative: Using foreach Loop

While GetEnumerator() provides explicit control over iteration, you can also use the simpler foreach loop which internally uses the enumerator −

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>();
      sortedDict.Add("Charlie", 25);
      sortedDict.Add("Alice", 30);
      sortedDict.Add("Bob", 22);
      Console.WriteLine("Using foreach loop:");
      foreach (KeyValuePair<string, int> kvp in sortedDict) {
         Console.WriteLine("Key = " + kvp.Key + ", Value = " + kvp.Value);
      }
   }
}

The output of the above code is −

Using foreach loop:
Key = Alice, Value = 30
Key = Bob, Value = 22
Key = Charlie, Value = 25

Conclusion

The GetEnumerator() method provides explicit control over iteration through a SortedDictionary, returning an IDictionaryEnumerator that maintains the sorted order of keys. While foreach loops are more convenient, using GetEnumerator() directly gives you finer control over the enumeration process.

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

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements