ECMA-334: 26.3 Enumerable objects
|C# Language Specification|
|© 2006 ECMA International|
26.3 Enumerable objects
When a function member returning an enumerable interface type is implemented using an iterator block,
invoking the function member does not immediately execute the code in the iterator block. Instead, an
enumerable object is created and returned. The enumerable object’s
GetEnumerator method returns an enumerator object that encapsulates the code specified in the iterator block, and execution of the code in the
iterator block occurs when the enumerator object’s
MoveNext method is invoked. An enumerable object has
the following characteristics:
- It implements
Tis the yield type of the iterator block.
- It implements
- It is initialized with a copy of the argument values (if any) and instance value passed to the function member.
An enumerable object is typically an instance of a compiler-generated enumerable class that encapsulates the code in the iterator block and implements the enumerable interfaces, but other methods of implementation are possible. If an enumerable class is generated by the compiler, that class shall be nested, directly or indirectly, in the class containing the function member, it shall have private accessibility, and it shall have a name reserved for compiler use (§9.4.2).
[Note: An enumerable object can implement more interfaces than those specified above. In particular, an
enumerable object can also implement
IEnumerator<T>, enabling it to serve as both
an enumerable and an enumerator. In that type of implementation, the first time an enumerable object’s
GetEnumerator method is invoked, the enumerable object itself is returned. Subsequent invocations of the
GetEnumerator, if any, return a copy of the enumerable object. Thus, each returned
enumerator has its own state, and changes in one enumerator will not affect another. end note]