Simple Iterators

You can return elements from iterator functions with multiple Yield invocations. The following code demonstrates how to perform a loop over an iterator that returns a collection of strings:

Iterator Function StringSeries() As IEnumerable(Of String)    Yield "First string"    Yield "Second string"    Yield "Third string"    '...End FunctionSub Main()    For Each item As String In StringSeries()        Console.WriteLine(item)    Next    Console.ReadLine()End Sub

You can also use a single Yield statement inside a For..Each or For..Next loop. The following code demonstrates how to retrieve the list of odd numbers in a sequence of integers:

Iterator Function OddNumbers(first ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.