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 StringDictionary in C#
The StringDictionary class in C# provides the GetEnumerator() method to obtain an enumerator that iterates through the collection. This enumerator returns DictionaryEntry objects containing key-value pairs. Note that StringDictionary automatically converts all keys to lowercase during storage.
Syntax
Following is the syntax for getting an enumerator from StringDictionary −
IEnumerator enumerator = stringDictionary.GetEnumerator();
To iterate through the enumerator −
while (enumerator.MoveNext()) {
DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
// Use entry.Key and entry.Value
}
Using GetEnumerator() Method
The following example demonstrates how to use GetEnumerator() to iterate through a StringDictionary −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
The output of the above code is −
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E
Using GetEnumerator() with Numbers
Here's another example showing enumerator usage with numeric string keys −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "One");
strDict.Add("2", "Two");
strDict.Add("3", "Three");
strDict.Add("4", "Four");
Console.WriteLine("StringDictionary key-value pairs...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
The output of the above code is −
StringDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four
Key Points
StringDictionary automatically converts all keys to lowercase during storage.
The
GetEnumerator()method returns anIEnumeratorobject.Each iteration returns a
DictionaryEntryobject with Key and Value properties.You can also use
foreachloop directly on StringDictionary for simpler iteration.
Conclusion
The GetEnumerator() method provides a way to iterate through StringDictionary elements using the IEnumerator interface. This method is useful when you need explicit control over iteration, though the foreach loop offers a more convenient alternative for most scenarios.
