dotNet Product

Solved: Why Linq Where.FirstOrDefault is faster than use FirstOrDefault with condition on an array?

1 min readNo comments yet

Hi all.

 

Why applying ".Where(condition).FirstOrDefault()" is much faster than ".FirstOrDefault(condition) when it's applied on an array?

 

Answer: While calling method Where() on an Array or a List, it's optimized to use the enumerator of the source directly. But FirstOrDefault(), which doesn't have this mechanism, runs the virtual method version from IEnumerator.GetEnumerator().

Special thanks to Iris Sakura.

Comments

Leave a comment

Comments are reviewed before they appear.