IDictionaryEnumerator Interface
The IDictionaryEnumerator
interface provides the ability to enumerate the elements of a IDictionary
collection. It extends IEnumerator
by exposing the key of each dictionary entry.
Namespace
System.Collections
Assembly
System.Collections.dll
Syntax
public interface IDictionaryEnumerator : IEnumerator
{
DictionaryEntry Entry { get; }
object Key { get; }
object Value { get; }
}
Members
Member | Type | Description |
---|---|---|
Entry |
DictionaryEntry |
Gets both the key and the value of the current dictionary entry. |
Key |
object |
Gets the key of the current dictionary entry. |
Value |
object |
Gets the value of the current dictionary entry. |
Current |
object |
Inherited from IEnumerator . Gets the current element in the collection. |
MoveNext() |
bool |
Advances the enumerator to the next element of the collection. |
Reset() |
void |
Sets the enumerator to its initial position, which is before the first element in the collection. |
Remarks
When iterating through a dictionary, IDictionaryEnumerator
provides direct access to both the key and value without needing to cast the Current
property to a DictionaryEntry
. This makes code more readable and type‑safe.
Example
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable table = new Hashtable();
table["apple"] = "red";
table["banana"] = "yellow";
table["grape"] = "purple";
IDictionaryEnumerator enumerator = table.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine($"Key: {enumerator.Key}, Value: {enumerator.Value}");
}
}
}
// Output:
// Key: apple, Value: red
// Key: banana, Value: yellow
// Key: grape, Value: purple