Collection Classes
- slide 36 : 36
Iterator blocks and yield return
Iterators are implemented by methods made with iterator blocks
IEnumerator<Type> Method() {
// iterator block
Type t; ... yield return t; ... }
Characteristics
The return type of the method must be an enumerator or an enumerable
A method with an iterator block is not executed immediately when calling it
Instead an
enumerable object
is created and returned
Execution starts when
MoveNext
is activated on the enumerable object
The compiler transforms a method with an iterator block to an underlying state machine which manages the traversal
Infinite Collections of Integers