3 ms·
Do you know why that is? That's very interesting.
by joe_guy 5y ago
Do you know why that is? That's very interesting.
- sbelskie 5y agoNot sure if it is still the case, but it used to be that First did a fairly naive foreach over the IEnumerable while Where has several collection specific type checks that allow it to use MoveNext and maybe other more efficient ways to traverse the collection.
- foepys 5y agoLINQ is parsing a tree of System.Linq.Expression here and the cases of First(pred) etc. are just not optimized because of the added complexity with little benefit. It only recently became a problem when Visual Studio got a new built-in analyzer that tells people to "optimize" this.
- sobani 5y agoIf you're using LINQ on a list, it will not use Expressions, but a plain Func. So nothing will get parsed either way. The difference, as @sbelskie already mentioned, is that Where has an optimization for List, while First only uses the naive enumerator.