Enumerables, Iterators and Yield
Enumerators
An enumerator works over a quence, and is considered to have a concept of “before the first element”, the t of elements, and another concept of “after the last element”
An enumerator also has a current position, which is changed by calling the MoveNext method
An enumerator's Current property returns the element at the current position, or if accesd when the current position is “before the first element”
太学士
or “after the last element”, throws an exception
...
Before the first element After the last element Current position
Enumerators
System.Collections.IEnumerable has one method
●IEnumerator GetEnumerator();
System.Collections.IEnumerator has two methods and one property
●object Current: returns reference to the element at the current position
(note the return type is object [not type safe])
●void Ret(): rets the current position to “before the first element”
中级论文●bool MoveNext(): either changes current to refer to the next element (and returns true),
or “after the last element” if no more available (and returns fal)
[Enumerator is initialid to “before the first element”, so MoveNext must be called to have Current refer to first element]
Enumerators
also has type-safe generic versions of enumerable and enumerator ●System.Collections.Generic.IEnumerable<T> has one method萨莫耶
曲组词
●IEnumerator<T> GetEnumerator();
院感知识培训
System.Collections.Generic.IEnumerator<T> had one method and one property
●bool MoveNext(): advances the current position
●T Current: returns type-safe reference to the element at the current position Note there is no Ret method
Notes on Enumerators
苏教版五年级数学Enumerators are read-only
Ud to read elements, but not to t them
Not synchronid
玫瑰与夜莺foreach keyword in C# and for each in VB requires that a collection supports the IEnumerable interface
蛇和什么生肖最配
An enumerator is only valid while the underlying quence does not change C# iterators work with the Enumerator concept