DoubleCollection.GetEnumerator 方法

定義

傳回可在集合中逐一查看列舉值。Returns an enumerator that can iterate through the collection.

public:
 System::Windows::Media::DoubleCollection::Enumerator GetEnumerator();
public System.Windows.Media.DoubleCollection.Enumerator GetEnumerator ();
member this.GetEnumerator : unit -> System.Windows.Media.DoubleCollection.Enumerator
Public Function GetEnumerator () As DoubleCollection.Enumerator

傳回

DoubleCollection.Enumerator,可以逐一查看集合。An DoubleCollection.Enumerator that can iterate through the collection.

備註

枚舉器只允許讀取集合中的資料。Enumerators only allow reading the data in the collection. 枚舉器不能用來修改基礎集合。Enumerators cannot be used to modify the underlying collection.

一開始, 列舉值會放置在集合中的第一個專案之前。Initially, the enumerator is positioned before the first item in the collection. 在這個位置, 呼叫Current會擲回例外狀況。At this position, calling Current throws an exception. 因此, 在讀取的MoveNext Current值之前, 您必須呼叫, 將枚舉器前移至集合的第一個專案。Therefore, you must call MoveNext to advance the enumerator to the first item of the collection before reading the value of Current.

Current 會傳回相同的物件直到呼叫 MoveNextResetCurrent returns the same object until either MoveNext or Reset is called. MoveNextCurrent設定為下一個專案。MoveNext sets Current to the next item.

在傳遞集合結尾之後, 列舉值會放置在集合中的最後一個專案之後, 而呼叫MoveNext false會傳回。After the end of the collection is passed, the enumerator is positioned after the last item in the collection, and calling MoveNext returns false. 如果最後一個呼叫MoveNext傳回false, 則呼叫Current會擲回例外狀況。If the last call to MoveNext returned false, calling Current throws an exception. 若要Current再次將設定為集合的第一個專案, 您可以Reset呼叫, MoveNext後面接著。To set Current to the first item of the collection again, you can call Reset followed by MoveNext.

只要集合維持不變,列舉程式就持續有效。An enumerator remains valid as long as the collection remains unchanged. 如果對集合進行了變更, 例如加入、修改或刪除專案, 枚舉器會失效, 且下一次呼叫MoveNextReset InvalidOperationException會擲回。If changes are made to the collection, such as adding, modifying or deleting items, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. 如果在和MoveNext Current之間修改集合, Current則會傳回其設定的專案, 即使列舉值已經無效也一樣。If the collection is modified between MoveNext and Current, Current will return the item that it is set to, even if the enumerator is already invalidated.

列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. 即使集合已同步處理, 其他執行緒仍然可以修改集合, 這會導致列舉值擲回例外狀況。Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

適用於

另請參閱