Rediger

Del via


Hashtable.GetEnumerator Method

Definition

Returns an IDictionaryEnumerator that iterates through the Hashtable.

public:
 virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Overridable Function GetEnumerator () As IDictionaryEnumerator

Returns

An IDictionaryEnumerator for the Hashtable.

Implements

Examples

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

using namespace System;
using namespace System::Collections;

public class HashtableExample
{
public:
    static void Main()
    {
        // Creates and initializes a new Hashtable.
        Hashtable^ clouds = gcnew Hashtable();
        clouds->Add("Cirrus", "Castellanus");
        clouds->Add("Cirrocumulus", "Stratiformis");
        clouds->Add("Altostratus", "Radiatus");
        clouds->Add("Stratocumulus", "Perlucidus");
        clouds->Add("Stratus", "Fractus");
        clouds->Add("Nimbostratus", "Pannus");
        clouds->Add("Cumulus", "Humilis");
        clouds->Add("Cumulonimbus", "Incus");

        // Displays the keys and values of the Hashtable using GetEnumerator()

        IDictionaryEnumerator^ denum = clouds->GetEnumerator();
        DictionaryEntry dentry;

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

        // Displays the keys and values of the Hashtable using foreach statement

        Console::WriteLine("    Cloud Type       Variation");
        Console::WriteLine("    -----------------------------");
        for each (DictionaryEntry de in clouds)
        {
            Console::WriteLine("    {0,-17}{1}", de.Key, de.Value);
        }
        Console::WriteLine();
    }
};

int main()
{
    HashtableExample::Main();
}

// The program displays the following output to the console:
//
//    Cloud Type       Variation
//    -----------------------------
//    Cirrocumulus     Stratiformis
//    Stratocumulus    Perlucidus
//    Cirrus           Castellanus
//    Cumulus          Humilis
//    Nimbostratus     Pannus
//    Stratus          Fractus
//    Altostratus      Radiatus
//    Cumulonimbus     Incus
//
//    Cloud Type       Variation
//    -----------------------------
//    Cirrocumulus     Stratiformis
//    Stratocumulus    Perlucidus
//    Cirrus           Castellanus
//    Cumulus          Humilis
//    Nimbostratus     Pannus
//    Stratus          Fractus
//    Altostratus      Radiatus
//    Cumulonimbus     Incus*/
using System;
using System.Collections;

public class HashtableExample
{
    public static void Main()
    {
        // Creates and initializes a new Hashtable.
        Hashtable clouds = new Hashtable();
        clouds.Add("Cirrus", "Castellanus");
        clouds.Add("Cirrocumulus", "Stratiformis");
        clouds.Add("Altostratus", "Radiatus");
        clouds.Add("Stratocumulus", "Perlucidus");
        clouds.Add("Stratus", "Fractus");
        clouds.Add("Nimbostratus", "Pannus");
        clouds.Add("Cumulus", "Humilis");
        clouds.Add("Cumulonimbus", "Incus");

        // Displays the keys and values of the Hashtable using GetEnumerator()

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

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

        // Displays the keys and values of the Hashtable using foreach statement

        Console.WriteLine("    Cloud Type       Variation");
        Console.WriteLine("    -----------------------------");
        foreach (DictionaryEntry de in clouds)
        {
            Console.WriteLine("    {0,-17}{1}", de.Key, de.Value);
        }
        Console.WriteLine();
    }
}

// The program displays the following output to the console:
//
//    Cloud Type       Variation
//    -----------------------------
//    Cirrocumulus     Stratiformis
//    Stratocumulus    Perlucidus
//    Cirrus           Castellanus
//    Cumulus          Humilis
//    Nimbostratus     Pannus
//    Stratus          Fractus
//    Altostratus      Radiatus
//    Cumulonimbus     Incus
//
//    Cloud Type       Variation
//    -----------------------------
//    Cirrocumulus     Stratiformis
//    Stratocumulus    Perlucidus
//    Cirrus           Castellanus
//    Cumulus          Humilis
//    Nimbostratus     Pannus
//    Stratus          Fractus
//    Altostratus      Radiatus
//    Cumulonimbus     Incus*/
Imports System.Collections

Public Class HashtableExample
    Public Shared Sub Main()
        ' Creates and initializes a new Hashtable.
        Dim clouds As New Hashtable()
        clouds.Add("Cirrus", "Castellanus")
        clouds.Add("Cirrocumulus", "Stratiformis")
        clouds.Add("Altostratus", "Radiatus")
        clouds.Add("Stratocumulus", "Perlucidus")
        clouds.Add("Stratus", "Fractus")
        clouds.Add("Nimbostratus", "Pannus")
        clouds.Add("Cumulus", "Humilis")
        clouds.Add("Cumulonimbus", "Incus")

        ' Displays the keys and values of the Hashtable using GetEnumerator()

        Dim denum As IDictionaryEnumerator = clouds.GetEnumerator()
        Dim dentry As DictionaryEntry

        Console.WriteLine()
        Console.WriteLine("    Cloud Type       Variation")
        Console.WriteLine("    -----------------------------")
        While denum.MoveNext()
            dentry = CType(denum.Current, DictionaryEntry)
            Console.WriteLine("    {0,-17}{1}", dentry.Key, dentry.Value)
        End While
        Console.WriteLine()

        ' Displays the keys and values of the Hashtable using foreach statement

        Console.WriteLine("    Cloud Type       Variation")
        Console.WriteLine("    -----------------------------")
        For Each de As DictionaryEntry in clouds
            Console.WriteLine("    {0,-17}{1}", de.Key, de.Value)
        Next de
        Console.WriteLine()
    End Sub
End Class

' The program displays the following output to the console:
'
'    Cloud Type       Variation
'    -----------------------------
'    Cirrocumulus     Stratiformis
'    Stratocumulus    Perlucidus
'    Cirrus           Castellanus
'    Cumulus          Humilis
'    Nimbostratus     Pannus
'    Stratus          Fractus
'    Altostratus      Radiatus
'    Cumulonimbus     Incus
'
'    Cloud Type       Variation
'    -----------------------------
'    Cirrocumulus     Stratiformis
'    Stratocumulus    Perlucidus
'    Cirrus           Castellanus
'    Cumulus          Humilis
'    Nimbostratus     Pannus
'    Stratus          Fractus
'    Altostratus      Radiatus
'    Cumulonimbus     Incus*/

Remarks

The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended, instead of directly manipulating the enumerator.

Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. At this position, Current is undefined. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, Current is undefined. To set Current to the first element of the collection again, you can call Reset followed by MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

This method is an O(1) operation.

Because serializing and deserializing an enumerator for a Hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the Reset method.

Applies to

See also