Performance of LINQ Any vs FirstOrDefault != null

后端 未结 7 1755
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 12:05

There are multiple places in an Open Source Project (OSP) code I contribute, where it has to be determined if an element in a collection satisfies a certain condition.

相关标签:
7条回答
  • 2020-11-28 12:28

    The enumeration in Any() stops as soon as it finds a matching item as well:

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any

    I would expect the performance to be very similar. Note that the FirstOrDefault version won't work with a collection of value types (since the default isn't null) but the Any version would.

    0 讨论(0)
提交回复
热议问题