Implementing an Iterator Class
Iterators can be useful in creating classes that act like lists. By implementing the IEnumerable
interface and the GetEnumerator
method of this interface, you can write efficient code that takes advantage of iterator functions to yield the content of a list. Listing 16.2 demonstrates how to create a class called BottlesOfWine
that stores a list of bottles of wine given a name and a color of the wine (red or white). Internally it uses a List(Of BottleOfWine)
instance, but instead of invoking the usual GetEnumerator
method of the List
class (provided by the IEnumerable
interface that the List
implements), it returns the list of bottles via an iterator function. It also returns a list of bottles based on their color. ...
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.