Iterator (Visual Basic)Iterator (Visual Basic)

指定函數或 Get 存取子是反覆運算器。Specifies that a function or Get accessor is an iterator.

備註Remarks

反覆運算器會在集合上執行自訂反復專案。An iterator performs a custom iteration over a collection. 反覆運算器會使用Yield語句,一次傳回集合中的每個元素。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 修飾詞會出現在 iterator 函數或 Get 存取子的宣告中。The Iterator modifier appears in the declaration of the iterator function or Get accessor.

您可以從用戶端程式代碼呼叫反覆運算器,方法是使用For Each 。下一個語句You call an iterator from client code by using a For Each...Next Statement.

Iterator 函數或 Get 存取子的傳回類型可以是 IEnumerableIEnumerable<T>IEnumeratorIEnumerator<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.

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

使用方式Usage

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

範例Example

下列範例示範反覆運算器函數。The following example demonstrates an iterator function. Iterator 函數的 Yield 語句位於For 。下一個迴圈。The iterator function has a Yield statement that is inside a For…Next loop. Main個語句主體的每個反復專案都會建立對 Power iterator 函數的呼叫。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 & " ")
    Next
    ' Output: 2 4 8 16 32 64 128 256
    Console.ReadKey()
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
    Next
End Function

範例Example

下列範例將示範本身為迭代器的 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
    Next
    Console.ReadKey()
End Sub

Public Class Galaxies
    Public ReadOnly Iterator Property NextGalaxy _
    As System.Collections.Generic.IEnumerable(Of Galaxy)
        Get
            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