Model3DCollection.GetEnumerator Model3DCollection.GetEnumerator Model3DCollection.GetEnumerator Model3DCollection.GetEnumerator Method


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

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


可以逐一查看集合的列舉程式。An 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 , 將列舉值前移至集合的第一個專案, 然後再讀取目前的值。Therefore, you must call MoveNext to advance the enumerator to the first item of the collection before reading the value of Current.

Current 會傳回相同的物件, 直到呼叫Reset MoveNext 或為止。Current returns the same object until either MoveNext or Reset is called. MoveNext 會將目前的設定為下一個專案。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. 若要再次將 [最新] 設定為集合的第一個專案, 您可以呼叫 [重設], 後面接著 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. 如果對集合進行了變更, 例如加入、修改或刪除專案, 枚舉器會失效, 且下一次呼叫 MoveNext 或 Reset 會擲回 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 會傳回其設定的專案, 即使列舉值已經無效也一樣。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.