Among Find, Single, First, which one is the fastest?

前端 未结 5 1872
庸人自扰
庸人自扰 2020-12-30 23:28

I want to minimize the time needed to retrieve a single unique element from a list. Which one is the fastest method among Find, Single and Fi

5条回答
  •  长情又很酷
    2020-12-30 23:50

    They are different methods. Find is defined in List, it's almost the same as First that is defined in Enumerable.cs as an extension method on IEnumerable. Both of them will return if a conditioned item is found(no need to loop through the whole collection), so they have slight performance difference.

    While Single returns the conditioned item, and also guarantees that this item is the only one that meets the condition. So in most circumstances Single is slower than First/Find because it needs to loop through the collection.

提交回复
热议问题