What is the performance of the Last() extension method for List?

前端 未结 5 1346
慢半拍i
慢半拍i 2020-11-30 11:06

I really like Last() and would use it all the time for Lists. But since it seems to be defined for IEnumerable, I gu

5条回答
  •  死守一世寂寞
    2020-11-30 11:21

    Short answer:

    O(1).

    Explanation:

    It's evident that Last() for List uses Count() extension method.

    Count() checks type of the collection in runtime and uses Count property if it's available.

    Count property for list has O(1) complexity so is the Last() extension method.

提交回复
热议问题