Dictionary<TKey,TValue>.KeyCollection.Enumerator 結構

定義

public: value class Dictionary<TKey, TValue>::KeyCollection::Enumerator : System::Collections::Generic::IEnumerator<TKey>
public struct Dictionary<TKey,TValue>.KeyCollection.Enumerator : System.Collections.Generic.IEnumerator<TKey>
[System.Serializable]
public struct Dictionary<TKey,TValue>.KeyCollection.Enumerator : System.Collections.Generic.IEnumerator<TKey>
type Dictionary<'Key, 'Value>.KeyCollection.Enumerator = struct
    interface IEnumerator<'Key>
    interface IEnumerator
    interface IDisposable
[<System.Serializable>]
type Dictionary<'Key, 'Value>.KeyCollection.Enumerator = struct
    interface IEnumerator<'Key>
    interface IDisposable
    interface IEnumerator
[<System.Serializable>]
type Dictionary<'Key, 'Value>.KeyCollection.Enumerator = struct
    interface IEnumerator<'Key>
    interface IEnumerator
    interface IDisposable
Public Structure Dictionary(Of TKey, TValue).KeyCollection.Enumerator
Implements IEnumerator(Of TKey)

類型參數

TKey
TValue
繼承
Dictionary<TKey,TValue>.KeyCollection.Enumerator
屬性
實作

備註

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

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

一開始,列舉程式位在集合中的第一個項目之前。 在這個位置上,Current 並未定義。 您必須呼叫 MoveNext ,才能將列舉值前進至集合的第一個專案,再讀取 的值 Current

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

如果 MoveNext 傳遞集合的結尾,列舉值會放置在集合的最後一個專案後面,並 MoveNextfalse 回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false 。 如果最後一次 MoveNext 呼叫傳 false 回 , Current 則為未定義。 您不能再次將 Current 設定為集合的第一個項目;您必須建立新的列舉值執行個體。

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

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

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

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

屬性

Current

取得位於目前列舉值位置的項目。

方法

Dispose()

釋放 Dictionary<TKey,TValue>.KeyCollection.Enumerator 所使用的所有資源。

MoveNext()

將列舉值前移至 Dictionary<TKey,TValue>.KeyCollection 的下一個項目。

明確介面實作

IEnumerator.Current

取得位於目前列舉值位置的項目。

IEnumerator.Reset()

設定列舉值至它的初始位置,這是在集合中第一個項目之前。

適用於

另請參閱