OrderedDictionary.GetEnumerator Metoda

Definicja

IDictionaryEnumerator Zwraca obiekt, który iteruje po kolekcjiOrderedDictionary.

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

Zwraca

Obiekt IDictionaryEnumerator dla kolekcji OrderedDictionary .

Implementuje

Przykłady

W poniższym przykładzie kodu pokazano użycie GetEnumerator metody w celu wyświetlenia zawartości OrderedDictionary kolekcji w konsoli programu . W tym przykładzie GetEnumerator metoda służy do uzyskiwania obiektu przekazywanego IDictionaryEnumerator do metody, która wyświetla zawartość. Ten kod jest częścią większego przykładu kodu, który można wyświetlić pod adresem OrderedDictionary.

// Clear the OrderedDictionary and add new values
myOrderedDictionary->Clear();
myOrderedDictionary->Add("newKey1", "newValue1");
myOrderedDictionary->Add("newKey2", "newValue2");
myOrderedDictionary->Add("newKey3", "newValue3");

// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator^ myEnumerator =
    myOrderedDictionary->GetEnumerator();

Console::WriteLine(
    "{0}Displaying the entries of a \"new\" OrderedDictionary.",
    Environment::NewLine);

DisplayEnumerator(myEnumerator);
// Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear();
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");

// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator myEnumerator =
    myOrderedDictionary.GetEnumerator();

Console.WriteLine(
    "{0}Displaying the entries of a \"new\" OrderedDictionary.",
    Environment.NewLine);

DisplayEnumerator(myEnumerator);
' Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear()
myOrderedDictionary.Add("newKey1", "newValue1")
myOrderedDictionary.Add("newKey2", "newValue2")
myOrderedDictionary.Add("newKey3", "newValue3")

' Display the contents of the "new" Dictionary Imports an enumerator
Dim myEnumerator As IDictionaryEnumerator = _
    myOrderedDictionary.GetEnumerator()

Console.WriteLine( _
    "{0}Displaying the entries of a 'new' OrderedDictionary.", _
    Environment.NewLine)

DisplayEnumerator(myEnumerator)
// Displays the contents of the OrderedDictionary using its enumerator
static void DisplayEnumerator(IDictionaryEnumerator^ myEnumerator)
{
    Console::WriteLine("   KEY                       VALUE");
    while (myEnumerator->MoveNext())
    {
        Console::WriteLine("   {0,-25} {1}",
            myEnumerator->Key, myEnumerator->Value);
    }
}
// Displays the contents of the OrderedDictionary using its enumerator
public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
{
    Console.WriteLine("   KEY                       VALUE");
    while (myEnumerator.MoveNext())
    {
        Console.WriteLine("   {0,-25} {1}",
            myEnumerator.Key, myEnumerator.Value);
    }
}
' Displays the contents of the OrderedDictionary using its enumerator
Public Shared Sub DisplayEnumerator( _
    ByVal myEnumerator As IDictionaryEnumerator)

    Console.WriteLine("   KEY                       VALUE")
    While myEnumerator.MoveNext()
        Console.WriteLine("   {0,-25} {1}", _
            myEnumerator.Key, myEnumerator.Value)
    End While
End Sub

Uwagi

Instrukcja foreach języka C# (for each w Visual Basic) ukrywa złożoność modułów wyliczania. Dlatego użycie foreach metody jest zalecane zamiast bezpośredniego manipulowania modułem wyliczającym.

Moduły wyliczające mogą służyć do odczytu danych w kolekcji, ale nie można za ich pomocą modyfikować kolekcji źródłowej.

Początkowo moduł wyliczający jest umieszczony przed pierwszym elementem w kolekcji.

Moduł wyliczający zachowuje ważność tak długo, jak długo kolekcja pozostaje niezmieniona. Jeśli w kolekcji zostaną wprowadzone zmiany, takie jak dodanie, zmodyfikowanie czy usunięcie elementów, moduł wyliczający jest nieodwracalnie unieważniany, a jego zachowanie staje się niezdefiniowane.

Moduł wyliczający nie ma wyłącznego dostępu do kolekcji, w związku z tym wyliczanie w kolekcji nie jest wewnętrznie procedurą odporną na wielowątkowość. Aby zagwarantować bezpieczeństwo wątków podczas wyliczania, można zablokować kolekcję podczas całego procesu wyliczania. Aby zezwolić wielu wątkom na dostęp do kolekcji w celu odczytu i zapisu danych, należy zaimplementować własny mechanizm synchronizacji.

Ta metoda jest operacją O(1).

Dotyczy