Dictionary<TKey,TValue>.IDictionary.GetEnumerator 方法

定義

 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

傳回

IDictionaryEnumerator

IDictionaryEnumeratorIDictionary

實作

範例

下列程式碼範例示範如何在 C++) 中使用 foreach 語句 (For Each Visual Basic, for each 在 C++) 中列舉字典中的索引鍵/值組,以隱藏列舉值的用法。 特別是請注意,介面的 System.Collections.IDictionary 列舉值會傳回 DictionaryEntry 物件,而不是 KeyValuePair<TKey,TValue> 物件。

程式碼範例是較大範例的一部分,包括針對 IDictionary.Add 方法提供的輸出。

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");
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();
foreach( DictionaryEntry de in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        de.Key, de.Value);
}
' 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

備註

為了列舉的目的,每個專案都是 DictionaryEntry 結構。

foreachC++ 中 C# 語言 (for each 的語句, For Each Visual Basic) 隱藏列舉值的複雜度。 因此,建議您使用 foreach,而不要直接使用列舉值。

列舉程式可以用來讀取集合中的資料,但是無法用來修改基礎集合。

一開始,列舉程式位在集合中的第一個項目之前。 方法 Reset 也會將列舉值帶回這個位置。 在這個位置上,Entry 並未定義。 因此,您必須先呼叫 MoveNext 方法,將列舉值前移至集合的第一個專案,然後再讀取 的值 Entry

屬性 Entry 會傳回相同的專案, MoveNext 直到呼叫 或 Reset 方法為止。 MoveNext 會將 Entry 設定為下一個項目。

如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNextfalse 回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false 。 如果最後一 MoveNext 次呼叫傳 false 回 , Entry 則為未定義。 若要再次將 Entry 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入專案或變更容量,列舉值就會無法復原,而下一次呼叫 MoveNextIEnumerator.ResetInvalidOperationException 擲回 。

僅限 .NET Core 3.0+ :唯一不會使列舉值失效的變動方法為 RemoveClear

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。

System.Collections.Generic 命名空間中集合的預設實作未同步處理。

這個方法是 O (1) 作業。

適用於

另請參閱