語言

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

定義

回傳 和 IDictionaryEnumerator 表示 IDictionary。

 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

傳回

A IDictionaryEnumerator 代表 IDictionary。

實作

範例

以下程式碼範例展示了如何使用 (Visual Basic 中的 陳述句)來枚舉字典 foreach 中的鍵/值對,該陳述For Each 式隱藏了列舉器的使用。 特別注意,介面的 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 sorted dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedDictionary<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 sorted dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New SortedDictionary(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 結構。

foreach C# 語言(For EachVisual Basic 中)的陳述隱藏了列舉器的複雜性。 因此,建議使用 foreach ,而非直接操作列舉器。

列舉器可用於讀取集合中的資料,但無法用來修改底層集合。

最初,列舉器會被放置在集合中的第一個元素之前。 此 Reset 方法也會將調查員拉回此位置。 在此位置, Entry 未定義。 因此,你必須呼叫 MoveNext 方法,將列舉子推進到集合的第一個元素,才能讀取 的 Entry值。

該 Entry 屬性會回傳相同的物件,直到呼叫其中 MoveNext or Reset 為止。 MoveNext 設定 Entry 為下一個元素。

若 MoveNext 通過集合的末尾,列舉器會位於集合的最後一個元素之後,並 MoveNext 返回 false。 當列舉器處於此位置時,後續呼叫 MoveNext 也返回 false。 若最後一次呼叫 MoveNext 回傳 false, Entry 則為未定義。 若要再次設定 Entry 為集合的第一個元素,你可以呼叫 Reset ,接著 MoveNext。

只要集合未曾改變,列舉器仍然有效。 若對集合進行變更,例如新增、修改或刪除元素,列舉子將無法恢復地失效,下一次呼叫 MoveNext 或 Reset 會拋 InvalidOperationException出 。

列舉值沒有集合的獨佔存取權;因此,透過集合列舉本質上不是安全線程的程式。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合。 若要允許多個線程存取集合以進行讀取和寫入,您必須實作自己的同步處理。

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

此方法是一種 O(log n) 運算,其中 n 是集合中的元素數量。

適用於

另請參閱