Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get an enumerator that iterates through the Dictionary in C#
To get an enumerator that iterates through a Dictionary in C#, you use the GetEnumerator() method which returns an IDictionaryEnumerator. This enumerator provides access to both keys and values as you iterate through the dictionary's key-value pairs.
Syntax
Following is the syntax for getting a Dictionary enumerator −
IDictionaryEnumerator enumerator = dictionary.GetEnumerator();
The enumerator is used with MoveNext() to advance through the collection −
while (enumerator.MoveNext()) {
Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value);
}
Using Dictionary Enumerator with Integer Keys
Example
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(100, "Laptop");
dict.Add(150, "Desktop");
dict.Add(200, "Earphone");
dict.Add(300, "Tablet");
dict.Add(500, "Speakers");
dict.Add(750, "HardDisk");
dict.Add(1000, "SSD");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
Console.WriteLine("Enumerator iterating key-value pairs...");
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
The output of the above code is −
Enumerator iterating key-value pairs... Key = 100, Value = Laptop Key = 150, Value = Desktop Key = 200, Value = Earphone Key = 300, Value = Tablet Key = 500, Value = Speakers Key = 750, Value = HardDisk Key = 1000, Value = SSD
Using Dictionary Enumerator with String Keys
Example
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "Laptop");
dict.Add("Two", "Desktop");
dict.Add("Three", "Earphone");
dict.Add("Four", "Tablet");
dict.Add("Five", "Speakers");
dict.Add("Six", "HardDisk");
dict.Add("Seven", "SSD");
dict.Add("Eight", "Keyboard");
dict.Add("Nine", "Mouse");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
Console.WriteLine("Enumerator iterating key-value pairs...");
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
The output of the above code is −
Enumerator iterating key-value pairs... Key = One, Value = Laptop Key = Two, Value = Desktop Key = Three, Value = Earphone Key = Four, Value = Tablet Key = Five, Value = Speakers Key = Six, Value = HardDisk Key = Seven, Value = SSD Key = Eight, Value = Keyboard Key = Nine, Value = Mouse
Alternative Iteration Methods
While GetEnumerator() provides fine-grained control, C# offers more convenient ways to iterate through dictionaries −
Using foreach with KeyValuePair
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "First");
dict.Add(2, "Second");
dict.Add(3, "Third");
Console.WriteLine("Using foreach with KeyValuePair:");
foreach (KeyValuePair<int, string> pair in dict) {
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
}
}
The output of the above code is −
Using foreach with KeyValuePair: Key = 1, Value = First Key = 2, Value = Second Key = 3, Value = Third
Key Properties of IDictionaryEnumerator
| Property | Description |
|---|---|
Key |
Gets the key of the current dictionary entry. |
Value |
Gets the value of the current dictionary entry. |
Entry |
Gets both key and value as a DictionaryEntry object. |
Conclusion
The GetEnumerator() method returns an IDictionaryEnumerator that allows manual iteration through Dictionary key-value pairs using MoveNext(). While this approach provides control over the iteration process, modern C# applications typically use foreach loops with KeyValuePair for simpler and more readable code.
