Iterator (Visual Basic)Iterator (Visual Basic)

指定函式或Get存取子是迭代器。Specifies that a function or Get accessor is an iterator.


迭代器對集合執行自訂反覆項目。An iterator performs a custom iteration over a collection. 迭代器會使用產生陳述式來傳回一次一個集合中的每個項目。An iterator uses the Yield statement to return each element in the collection one at a time. Yield到達陳述式時,保留在程式碼中的目前位置。When a Yield statement is reached, the current location in code is retained. 下一次呼叫 Iterator 函式時,便會從這個位置重新開始執行。Execution is restarted from that location the next time that the iterator function is called.

迭代器可以實作為函式或Get屬性定義存取子。An iterator can be implemented as a function or as a Get accessor of a property definition. Iterator修飾詞會出現在迭代器函式宣告或Get存取子。The Iterator modifier appears in the declaration of the iterator function or Get accessor.

從用戶端程式碼呼叫迭代器使用每個...下一個陳述式You call an iterator from client code by using a For Each...Next Statement.

迭代器函式的傳回型別或Get存取子可以是IEnumerableIEnumerable<T>IEnumerator,或IEnumerator<T>The return type of an iterator function or Get accessor can be IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.

迭代器不能有任何ByRef參數。An iterator cannot have any ByRef parameters.

迭代器不能出現在事件、執行個體建構函式、靜態建構函式或靜態解構函式中。An iterator cannot occur in an event, instance constructor, static constructor, or static destructor.

迭代器可以是匿名函式。An iterator can be an anonymous function. 如需詳細資訊,請參閱 IteratorFor more information, see Iterators.


Iterator 修飾詞可用於以下內容:The Iterator modifier can be used in these contexts:


下列範例示範的迭代器函式。The following example demonstrates an iterator function. 迭代器函式具有Yield內的陳述式For...下一步迴圈。The iterator function has a Yield statement that is inside a For…Next loop. 每次反覆運算針對每個中的陳述式主體Main建立呼叫Power迭代器函式。Each iteration of the For Each statement body in Main creates a call to the Power iterator function. 每次呼叫 Iterator 函式都會執行下一個 Yield 陳述式,這會在 For…Next 迴圈的下一個反覆項目期間發生。Each call to the iterator function proceeds to the next execution of the Yield statement, which occurs during the next iteration of the For…Next loop.

Sub Main()
    For Each number In Power(2, 8)
        Console.Write(number & " ")
    ' Output: 2 4 8 16 32 64 128 256
End Sub

Private Iterator Function Power(
ByVal base As Integer, ByVal highExponent As Integer) _
As System.Collections.Generic.IEnumerable(Of Integer)

    Dim result = 1

    For counter = 1 To highExponent
        result = result * base
        Yield result
End Function


下列範例將示範本身為迭代器的 Get 存取子。The following example demonstrates a Get accessor that is an iterator. Iterator修飾詞是屬性宣告中。The Iterator modifier is in the property declaration.

Sub Main()
    Dim theGalaxies As New Galaxies
    For Each theGalaxy In theGalaxies.NextGalaxy
        With theGalaxy
            Console.WriteLine(.Name & "  " & .MegaLightYears)
        End With
End Sub

Public Class Galaxies
    Public ReadOnly Iterator Property NextGalaxy _
    As System.Collections.Generic.IEnumerable(Of Galaxy)
            Yield New Galaxy With {.Name = "Tadpole", .MegaLightYears = 400}
            Yield New Galaxy With {.Name = "Pinwheel", .MegaLightYears = 25}
            Yield New Galaxy With {.Name = "Milky Way", .MegaLightYears = 0}
            Yield New Galaxy With {.Name = "Andromeda", .MegaLightYears = 3}
        End Get
    End Property
End Class

Public Class Galaxy
    Public Property Name As String
    Public Property MegaLightYears As Integer
End Class

如需其他範例,請參閱 < 迭代器For additional examples, see Iterators.

另請參閱See also