-1

How effective is using Last() for arrays?

var array = new[] { ... };
var last = array.Last(); // or array[array.Length - 1]

In sources it only distinguish IList<T>, so is that true what Last() will enumerate a complete array to return last item? Funny stuff is msdn example without a single note.

Sinatr
  • 18,856
  • 9
  • 75
  • 248
  • `T[]` implements `IList` (See even this response to the question linked by fubo: http://stackoverflow.com/a/1377891/613130) – xanatos Apr 12 '17 at 09:17

1 Answers1

3

Array does implement IList<T>:

var array = new int[] { 1, 2, 3 };
var list = (IList<int>)array;

So Last will not enumerate all array but instead will do array[array.Length - 1] (with a check that array is not empty of course - it which case it will throw).

Evk
  • 84,454
  • 8
  • 110
  • 160