| title | ms.date | ms.prod | ms.suite | ms.technology | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Iterator (Visual Basic) |
2015-07-20 |
.net |
|
article |
|
|
69cb0b04-ac87-49d0-bcfe-810c0d60daff |
11 |
dotnet-bot |
dotnetcontent |
|
Iterator (Visual Basic)
Specifies that a function or Get accessor is an iterator.
Remarks
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. When a Yield statement is reached, the current location in code is retained. Execution is restarted from that location the next time that the iterator function is called.
An iterator can be implemented as a function or as a Get accessor of a property definition. 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.
The return type of an iterator function or Get accessor can be xref:System.Collections.IEnumerable, xref:System.Collections.Generic.IEnumerable%601, xref:System.Collections.IEnumerator, or xref:System.Collections.Generic.IEnumerator%601.
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. For more information, see Iterators.
For more information about iterators, see Iterators.
Usage
The Iterator modifier can be used in these contexts:
Example
The following example demonstrates an iterator function. The iterator function has a Yield statement that is inside a For…Next loop. Each iteration of the For Each statement body in Main creates a call to the Power iterator function. 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.
[!code-vbVbVbalrStatements#98]
Example
The following example demonstrates a Get accessor that is an iterator. The Iterator modifier is in the property declaration.
[!code-vbVbVbalrStatements#99]
For additional examples, see Iterators.
See Also
xref:System.Runtime.CompilerServices.IteratorStateMachineAttribute
Iterators
Yield Statement