There is the enumerable extension method
Take<TSource>(
IEnumerable<TSource> source,
int count
)
which takes the first count elements from the start.
Is there a way to take the elements from the end?
or even better a way to take the elements from an offset to the end?
Thanks
or
There is some overhead in doing this so a custom method would be better.
Update: A custom method
Update: Changed the code a littlebit with ideas from dtb´s answer 🙂
Comment to Bear: Look at this example:
You could potentially change the values of
lastFive2and therefore that approach can be unsafe or at least it´s not the functional way.To Bear:
What I meant about safe is this:
In these cases you would have to make a copy to be sure. But in most cases your way would be fine – and a little bit more efficient than this so +1 🙂
An idea is to use a queue which only have internal Enqueue etc.