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

傳回

IDictionaryEnumeratorIDictionary

實作

範例

下列程式代碼範例示範如何使用 Visual Basic for each 中的 語句 (,For Each在 C++) 中使用 語句來列舉排序列表中的foreach索引鍵/值組,以隱藏列舉值的用法。 特別是請注意,介面的 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

備註

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

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

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

Entry 會傳回相同的物件直到呼叫 MoveNextResetMoveNext 會將 Entry 設定為下一個項目。

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

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如新增、修改或刪除專案,列舉值會無法復原,而下一次InvalidOperationException呼叫 MoveNextReset 會擲回 。

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

中的 System.Collections.Generic 集合預設實作不會同步處理。

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

適用於

另請參閱