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 Hashtable in C#
To get an enumerator that iterates through the Hashtable in C#, you use the GetEnumerator() method. This method returns an IDictionaryEnumerator that provides key-value pair access to the hashtable elements.
Syntax
Following is the syntax for getting an enumerator from a Hashtable −
IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
The enumerator provides the following key members −
enumerator.MoveNext() // advances to next element, returns bool enumerator.Key // gets current key enumerator.Value // gets current value enumerator.Entry // gets current DictionaryEntry
Return Value
The GetEnumerator() method returns an IDictionaryEnumerator object that can iterate through the key-value pairs in the hashtable. The iteration order is not guaranteed and may vary between different runs.
Using GetEnumerator() with While Loop
The most common way to use an enumerator is with a while loop that calls MoveNext() −
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable(10);
hash.Add("1", "Apple");
hash.Add("2", "Banana");
hash.Add("3", "Cherry");
hash.Add("4", "Date");
hash.Add("5", "Elderberry");
hash.Add("6", "Fig");
hash.Add("7", "Grape");
hash.Add("8", "Honeydew");
hash.Add("9", "Kiwi");
hash.Add("10", "Lemon");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = " + hash.IsFixedSize);
Console.WriteLine("Count of entries in Hashtable = " + hash.Count);
Console.WriteLine("\nEnumerator to iterate through the Hashtable...");
IDictionaryEnumerator demoEnum = hash.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
hash.Clear();
}
}
The output of the above code is −
Hashtable Key and Value pairs... 10 and Lemon 1 and Apple 2 and Banana 3 and Cherry 4 and Date 5 and Elderberry 6 and Fig 7 and Grape 8 and Honeydew 9 and Kiwi Is the Hashtable having fixed size? = False Count of entries in Hashtable = 10 Enumerator to iterate through the Hashtable... Key = 10, Value = Lemon Key = 1, Value = Apple Key = 2, Value = Banana Key = 3, Value = Cherry Key = 4, Value = Date Key = 5, Value = Elderberry Key = 6, Value = Fig Key = 7, Value = Grape Key = 8, Value = Honeydew Key = 9, Value = Kiwi
Using Entry Property
You can also access the current element as a DictionaryEntry using the Entry property −
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("One", "Monday");
hash.Add("Two", "Tuesday");
hash.Add("Three", "Wednesday");
hash.Add("Four", "Thursday");
hash.Add("Five", "Friday");
hash.Add("Six", "Saturday");
Console.WriteLine("Using Entry property to access DictionaryEntry...");
IDictionaryEnumerator demoEnum = hash.GetEnumerator();
while (demoEnum.MoveNext()) {
DictionaryEntry entry = demoEnum.Entry;
Console.WriteLine("Entry: {0} = {1}", entry.Key, entry.Value);
}
hash.Clear();
}
}
The output of the above code is −
Using Entry property to access DictionaryEntry... Entry: One = Monday Entry: Five = Friday Entry: Three = Wednesday Entry: Two = Tuesday Entry: Four = Thursday Entry: Six = Saturday
Key Rules
-
The enumerator does not guarantee any specific order of elements.
-
You must call
MoveNext()before accessingKey,Value, orEntry. -
The enumerator becomes invalid if the hashtable is modified during enumeration.
-
Multiple enumerators can exist for the same hashtable simultaneously.
Conclusion
The GetEnumerator() method provides a way to iterate through all key-value pairs in a Hashtable using an IDictionaryEnumerator. While the iteration order is not guaranteed, this approach gives you fine-grained control over the enumeration process compared to foreach loops.
