C# Hashtable GetEnumerator

Description

Hashtable GetEnumerator returns an IDictionaryEnumerator that iterates through the Hashtable.

Syntax

Hashtable.GetEnumerator has the following syntax.


public virtual IDictionaryEnumerator GetEnumerator()

Returns

Hashtable.GetEnumerator method returns An IDictionaryEnumerator for the Hashtable.

Example

The following example compares the use of GetEnumerator and foreach to enumerate the contents of a Hashtable.


using System;/*from   w  ww .j  a v  a 2 s. c o m*/
using System.Collections;

public class HashtableExample
{
    public static void Main()
    {
        Hashtable myHT = new Hashtable();
        myHT.Add("1", "4");
        myHT.Add("2", "5");
        myHT.Add("3", "6");

        IDictionaryEnumerator denum = myHT.GetEnumerator();
        DictionaryEntry dentry;

        while (denum.MoveNext())
        {
            dentry = (DictionaryEntry) denum.Current;
            Console.WriteLine("    {0,-17}{1}", dentry.Key, dentry.Value);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack