Does the .Last() extension method take into account if it’s called on an IList? I’m just wondering if there’s a significant performance difference between these:
IList<int> numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int lastNumber1 = numbers.Last();
int lastNumber2 = numbers[numbers.Count-1];
Intuition tells me that the first alternative is O(n) but the second is O(1). Is .Last() “smart” enough to try casting it to an IList?
Probably not, as it can do
list[list.count-1]Verified by reflector: