Dictionary<TKey,TValue>.IDictionary.GetEnumerator Metoda

Definice

IDictionaryEnumerator Vrátí hodnotu pro IDictionaryhodnotu .

 virtual System::Collections::IDictionaryEnumerator ^ System.Collections.IDictionary.GetEnumerator() = System::Collections::IDictionary::GetEnumerator;
System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator();
abstract member System.Collections.IDictionary.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.System.Collections.IDictionary.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Function GetEnumerator () As IDictionaryEnumerator Implements IDictionary.GetEnumerator

Návraty

A IDictionaryEnumerator pro IDictionary.

Implementuje

Příklady

Následující příklad kódu ukazuje, jak vytvořit výčet párů klíč/hodnota ve slovníku pomocí foreach příkazu (For Each v jazyce Visual Basic), který skryje použití enumerátoru. Všimněte si zejména, že enumerátor rozhraní System.Collections.IDictionary vrací DictionaryEntry objekty, nikoli KeyValuePair<TKey,TValue> objekty.

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new Dictionary<string, string>();

        // Add some elements to the dictionary. There are no
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // When you use foreach to enumerate dictionary elements
        // with the IDictionary interface, the elements are retrieved
        // as DictionaryEntry objects instead of KeyValuePair objects.
        foreach( DictionaryEntry de in openWith )
        {
            Console.WriteLine($"Key = {de.Key}, Value = {de.Value}");
        }
    }
}
open System
open System.Collections
open System.Collections.Generic

// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
let openWith: IDictionary = Dictionary<string, string>()

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

// When you use foreach to enumerate dictionary elements
// with the IDictionary interface, the elements are retrieved
// as DictionaryEntry objects instead of KeyValuePair objects.
printfn ""

for de in openWith do
    let de = de :?> DictionaryEntry
    printfn $"For key = {de.Key}, value = {de.Value}"
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' When you use foreach to enumerate dictionary elements
        ' with the IDictionary interface, the elements are retrieved
        ' as DictionaryEntry objects instead of KeyValuePair objects.
        Console.WriteLine()
        For Each de As DictionaryEntry In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                de.Key, de.Value)
        Next 

    End Sub

End Class

Poznámky

Pro účely výčtu je každá položka strukturou DictionaryEntry .

Příkaz foreach jazyka C# (For Each v jazyce Visual Basic) skryje složitost výčtů. Proto se místo přímé manipulace s enumerátorem doporučuje použití foreach .

Enumerátory lze použít ke čtení dat v kolekci, ale nelze je použít k úpravě podkladové kolekce.

Na začátku je enumerátor umístěn před prvním prvkem v kolekci. Metoda Reset také vrátí enumerátor zpět na tuto pozici. V této pozici Entry není definováno. Proto je nutné volat metodu MoveNext pro přechod enumerátoru na první prvek kolekce před čtením hodnoty Entry.

Vlastnost Entry vrátí stejný prvek, dokud MoveNext není volána metoda nebo metoda Reset . MoveNext nastaví Entry na další prvek.

Pokud MoveNext předá konec kolekce, enumerátor je umístěn za posledním prvkem v kolekci a MoveNext vrátí .false Pokud je enumerátor na této pozici, následná volání vrátit MoveNext také false. Pokud se vrátí poslední volání MoveNextfalse, Entry není definováno. Chcete-li nastavit Entry na první prvek kolekce znovu, můžete volat Reset následovaný MoveNext.

Enumerátor zůstane platný, dokud kolekce zůstane beze změny. Pokud jsou v kolekci provedeny změny, například přidání prvků nebo změna kapacity, enumerátor je nenávratně neplatný a další volání MoveNext nebo IEnumerator.Reset vyvolá výjimku InvalidOperationException.

Pouze .NET Core 3.0+ : Jediné ztlumené metody, které neaktivují výčet jsou Remove a Clear.

Enumerátor nemá výhradní přístup k kolekci; proto výčet prostřednictvím kolekce není vnitřně bezpečným postupem pro přístup z více vláken. Chcete-li zaručit bezpečnost vláken během výčtu, můžete kolekci uzamknout během celého výčtu. Pokud chcete povolit přístup ke kolekci více vlákny pro čtení a zápis, musíte implementovat vlastní synchronizaci.

Výchozí implementace kolekcí v System.Collections.Generic oboru názvů nejsou synchronizovány.

Tato metoda je operace O(1).

Platí pro

Viz také