語言

SortedList<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。

實作

範例

以下程式碼範例展示了如何透過使用 foreach 陳述式(For Each 在 Visual Basic 中)來枚舉排序清單中的鍵值對,該陳述式隱藏了列舉器的使用。 特別注意,介面的 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 list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string, string>();

        // Add some elements to the sorted list. 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 list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String, String)
        
        ' Add some elements to the sorted list. 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 sorted list 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 sorted list 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

備註

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

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

最初,列舉器會被放置在集合中的第一個元素之前。 Reset 同時也讓普查員回到這個位置。 在此位置, Entry 未定義。 因此,您必須先呼叫 MoveNext 列舉器先將列舉器推進到集合的第一個元素,再讀取 的 Entry值。

Entry 回傳相同物件,直到被呼叫其中一 MoveNext 或 Reset 。 MoveNext 設定 Entry 為下一個元素。

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

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

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

集合的預設實作 System.Collections.Generic 並未同步。

此方法是 O(1) 作業。

適用於

另請參閱