IEnumerable<T> representing the "rest" of an IEnumerable<T> sequence
- by Henry Jackson
If I am walking through an IEnumerable<T>, is there any way to get a new IEnumerable<T> representing the remaining items after the current one.
For example, I would like to write an extension method IEnumerator<T>.Remaining():
IEnumerable<int> sequence = ...
IEnumerator<int> enumerator = sequence.GetEnumerator();
if (enumerator.MoveNext() && enumerator.MoveNext()) {
IEnumerable<int> rest = enumerator.Remaining();
// 'rest' would contain elements in 'sequence' start at the 3rd element
}
I'm thinking of the collection of a sort of singly-linked list, so there should be a way to represent any remaining elements, right? I don't see any way to do this exposed on either IEnumerable<T> or IEnumerator<T>, so maybe it's incompatible with the notion of a potentially unbounded, nondeterministic sequence of elements.