Disclaimer: I understand the difference between IEnumerable<T>
and IEnumerator<T>
and how to use both. This is not a duplicate of this or this.
This is more of a design question - since IEnumerator<T>
already encapsulates all the necessary information (.Current
, .MoveNext()
) about something that can be enumerated, then what's the point of introducing a type (IEnumerable<T>
) whose sole purpose is to return an instance of the former?
To be specific:
Why can't
foreach
be designed to iterate directly through anIEnumerator<T>
, like so:// foreach (var e in anEnumerator) { //... } while (anEnumerator.MoveNext()) { doSomething(anEnumerator.Current); }
Why can't Linq be built based directly off of
IEnumerator<T>
?