OrderedDictionary.GetEnumerator 方法

定義

傳回會逐一查看 IDictionaryEnumerator 集合的 OrderedDictionary 物件。

public:
 virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Overridable Function GetEnumerator () As IDictionaryEnumerator

傳回

IDictionaryEnumerator 集合的 OrderedDictionary 物件。

實作

範例

下列程式代碼範例示範如何使用 GetEnumerator 方法,將集合的內容 OrderedDictionary 顯示至控制台。 在此範例中 GetEnumerator ,方法是用來取得 IDictionaryEnumerator 傳遞至顯示內容之方法的物件。 此程式代碼是可在 檢視 OrderedDictionary之較大程式碼範例的一部分。

// Clear the OrderedDictionary and add new values
myOrderedDictionary->Clear();
myOrderedDictionary->Add("newKey1", "newValue1");
myOrderedDictionary->Add("newKey2", "newValue2");
myOrderedDictionary->Add("newKey3", "newValue3");

// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator^ myEnumerator =
    myOrderedDictionary->GetEnumerator();

Console::WriteLine(
    "{0}Displaying the entries of a \"new\" OrderedDictionary.",
    Environment::NewLine);

DisplayEnumerator(myEnumerator);
// Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear();
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");

// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator myEnumerator =
    myOrderedDictionary.GetEnumerator();

Console.WriteLine(
    "{0}Displaying the entries of a \"new\" OrderedDictionary.",
    Environment.NewLine);

DisplayEnumerator(myEnumerator);
' Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear()
myOrderedDictionary.Add("newKey1", "newValue1")
myOrderedDictionary.Add("newKey2", "newValue2")
myOrderedDictionary.Add("newKey3", "newValue3")

' Display the contents of the "new" Dictionary Imports an enumerator
Dim myEnumerator As IDictionaryEnumerator = _
    myOrderedDictionary.GetEnumerator()

Console.WriteLine( _
    "{0}Displaying the entries of a 'new' OrderedDictionary.", _
    Environment.NewLine)

DisplayEnumerator(myEnumerator)
// Displays the contents of the OrderedDictionary using its enumerator
static void DisplayEnumerator(IDictionaryEnumerator^ myEnumerator)
{
    Console::WriteLine("   KEY                       VALUE");
    while (myEnumerator->MoveNext())
    {
        Console::WriteLine("   {0,-25} {1}",
            myEnumerator->Key, myEnumerator->Value);
    }
}
// Displays the contents of the OrderedDictionary using its enumerator
public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
{
    Console.WriteLine("   KEY                       VALUE");
    while (myEnumerator.MoveNext())
    {
        Console.WriteLine("   {0,-25} {1}",
            myEnumerator.Key, myEnumerator.Value);
    }
}
' Displays the contents of the OrderedDictionary using its enumerator
Public Shared Sub DisplayEnumerator( _
    ByVal myEnumerator As IDictionaryEnumerator)

    Console.WriteLine("   KEY                       VALUE")
    While myEnumerator.MoveNext()
        Console.WriteLine("   {0,-25} {1}", _
            myEnumerator.Key, myEnumerator.Value)
    End While
End Sub

備註

C# 語言的 foreach 陳述式 (在 Visual Basic 中為 for each) 會隱藏列舉值的複雜度。 因此,建議使用 foreach ,而不是直接操作列舉值。

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

一開始,列舉程式位在集合中的第一個項目之前。

只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入、修改或刪除項目,列舉程式會永久失效,且其行為未定義。

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

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

適用於