OrderedDictionary.GetEnumerator Metodo

Definizione

Restituisce un IDictionaryEnumerator oggetto che scorre l'insieme OrderedDictionary .

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

Valori restituiti

Oggetto IDictionaryEnumerator per l'insieme OrderedDictionary .

Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del GetEnumerator metodo per visualizzare il contenuto della OrderedDictionary raccolta nella console. In questo esempio, il GetEnumerator metodo viene utilizzato per ottenere un IDictionaryEnumerator oggetto passato a un metodo che visualizza il contenuto. Questo codice fa parte di un esempio di codice più ampio che può essere visualizzato in 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 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
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

Commenti

L'istruzione foreach del linguaggio C# (for each in Visual Basic) nasconde la complessità degli enumeratori. Pertanto, è consigliabile usare foreach anziché modificare direttamente l'enumeratore.

Gli enumeratori possono essere usati per leggere i dati nella raccolta, ma non possono essere usati per modificare la raccolta sottostante.

Inizialmente, l'enumeratore viene posizionato prima del primo elemento della raccolta.

Un enumeratore rimane valido finché la raccolta rimane invariata. Se vengono apportate modifiche alla raccolta, ad esempio l'aggiunta, la modifica o l'eliminazione di elementi, l'enumeratore viene invalidato in modo irreversibile e il relativo comportamento non è definito.

L'enumeratore non ha accesso esclusivo alla raccolta; pertanto, l'enumerazione tramite una raccolta non è intrinsecamente una procedura thread-safe. Per garantire la thread safety durante l'enumerazione, è possibile bloccare la raccolta durante l'intera enumerazione. Per consentire l'accesso alla raccolta da parte di più thread per la lettura e la scrittura, è necessario implementare la propria sincronizzazione.

Questo metodo è un'operazione O(1).

Si applica a