Say I have a simple array:
double[] myDoubleArray = new double[] { 0, 1, 2, 3, 4, 5 };
Is this as performant:
double last = myDoubleArray.Last();
as this?
double last = myDoubleArray[myDoubleArray.Length - 1];
Will Last() enumerate over the entire array even when it can make the above optimization?
If I passed some other IEnumerable (say one that was yielded), Last() would have to enumerate the sequence. I prefer using Last(), because code looks cleaner, but I would not make a sacrifice if it enumerates the sequence.
No, it won’t iterate all over elements. Here’s the code of Enumerable.Last() from reflector. As you see, it makes such optimization