Anonymous Iterators
Iterators can also be implemented as anonymous methods. These are discussed in more detail in Chapter 20, but for now all you need to know is that they are methods with no name and are defined on-the-fly inside a code block. The following code snippet demonstrates how to implement the previous iterator as an anonymous method:
'Type of oddSequence is inferred by the compilerDim oddSequence = Iterator Function(first As Integer, last As Integer) As IEnumerable(Of Integer) Try For number As Integer = first To last If number Mod 2 <> 0 Then 'is odd Yield ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access