Collection.GetEnumerator 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個遍歷集合的列舉器。
public:
System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator();
member this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
傳回
一個可以用來遍歷集合的列舉器。
範例
以下範例說明如何使用 GetEnumerator 物件的所有元素 Collection 。
Dim customers As New Collection
' Insert code to add elements to the customers collection.
Dim custEnum As IEnumerator = customers.GetEnumerator()
custEnum.Reset()
Dim thisCustomer As Object
While custEnum.MoveNext()
thisCustomer = custEnum.Current()
' Insert code to process this element of the collection.
End While
GetEnumerator 構造並回傳一個列舉器物件,實作 IEnumerator 命名空間的 System.Collections 介面。 列舉器物件會揭露屬性 Current 以及 MoveNext 和 Reset 方法。 如需詳細資訊,請參閱 For Each...Next 語句。
備註
每 一個......Next StatementGetEnumerator 呼叫取得一個列舉物件,以支援對集合元素的迭代。 通常,你會用 For Each...Next 迴圈來遍歷集合或陣列,不需要明確呼叫 GetEnumerator 。
如果你需要比......Next陳述更精確的迭代For Each控制,你可以用這個GetEnumerator方法來執行客製化的遍歷。 以下是一些你可能需要這麼做的情況。
你可能想回到集合的起點,在它完成前重新開始迭代。
你可能因為各種原因想跳過一個或多個元素。
你可能需要在穿越過程中改變集合的元素。 此時你必須取得新的枚舉器物件,因為之前的物件已被作廢。