IEnumerable<T> representing the "rest" of an IEnumerable<T> sequence

Posted by Henry Jackson on Stack Overflow See other posts from Stack Overflow or by Henry Jackson
Published on 2010-05-13T20:11:16Z Indexed on 2010/05/13 20:14 UTC
Read the original article Hit count: 273

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.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about ienumerable