IDictionaryEnumerator belongs to the System.Collections namespace. It provides simple iteration over non generics collection. You can read the data using IEnumeratorDictionary in Collection, but you can’t use it to modify the underlying data. It has four properties and two methods, which expose some functionality. Initially the IDictionaryEnumerator is position before the first element in the collections. It has a reset method, which brings the enumerator to that position. If you call current at this position, it will definitely throw an exception.
The move next method takes the enumerator to the first element in the collections, and then you can call the current method to read the value.
The enumerator remains valid until no modifications are made to the collections. If any modification such as addition or deletion are performed then there will an exception “InvalidOperationException”. So make sure no changes are made while using MoveNext and Current method.
IEnumerationDictionary exposes these properties and methods
It returns the current element in the collections. But make sure you have called MoveNext method before calling that method.
It advances the enumerator the position of the first element in the collection. As we discussed before enumerator resides before first element initially.
This method can be used to reset the position of enumerator to its initial position, which was before the first element in the collection.
This property Get the key of the current Dictionary entry.
This property Get the value of the current Dictionary entry.
This property Get both the key and the value of the current dictionary entry.
Hashtable is one of the collections. It has a key and paired value. It is organized on the bases of hash code of the key. Its namespace is System.Collections. It exposes a large numbers of use full methods. We will use some of the methods in our example.
A key cannot be null in the Hashtable but a value can be null. The capacity of the Hashtable is auto increased as the elements are added to the Hashtable with reallocation.
In this simple console Application we will demonstrate use of IDictionaryEnumerator and Hashtable in C#.
static void Main(string args)
// Hashtable of products having initial capacity of 1
Hashtable htProducts = new Hashtable(1);
htProducts.Add("111", "Apple Sauce");
// gets the enumerator
IDictionaryEnumerator ide = htProducts.GetEnumerator();
// Displaying values using Entry property
Debug.WriteLine("Displays value using Entry Property\n");
Console.Write("Product key: " + ide.Entry.Key + " : ");
Console.Write("Product Value: " + ide.Entry.Value);
// Resets the position
// Displaying value with out using entry property
Console.WriteLine("\nValues Directly using key and Value property\n");
Console.Write("Product Key: " + ide.Key + " : ");
Console.Write("Product Value: " + ide.Value);
Displays value using Entry Property
Product key: 222 : Product Value: Soda
Product key: 444 : Product Value: Biscuits
Product key: 111 : Product Value: Apple Sauce
Product key: 333 : Product Value: MatchBox
Values Direct using key and Value property
Product Key: 222 : Product Value: Soda
Product Key: 444 : Product Value: Biscuits
Product Key: 111 : Product Value: Apple Sauce
Product Key: 333 : Product Value: MatchBox
IDictionaryEnumerator Interface to enumerate Hashtable - C#