LINQ to Objects performance is a mixed thing. Since it involves a lot of indirected calls (every lambda is one), it is slower, sometimes (when the lambda captures and uses variables from its outer scope) 3-4x times so. And, no, the compiler (neither C# nor JIT) won't optimize it away - it cannot inline virtual method calls, and that's what a call via a delegate effectively is. So if you strictly want performance, foreach
is going to be slightly to significantly faster in any case.
PLINQ can alleviate the difference, but note that the penalty is significant enough that you might need 4 cores just to match up for it. On the other hand, it will scale up as number of cores increases, while a plain foreach
will not.