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 ...

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.