I like anonymous lambdas; they let you concisely express complex selects from lists. LINQ does something similar, so I thought I’d try it out (finally).
As someone who uses lambdas often to select subsets of collections, when should I use LINQ and when should I use lambdas? There are questions like this one which show a 10-100x difference in performance one way or the other.
LINQ evaluation is deferred. The expression for
primesandnumbers.Where(n => isOddAndNotDivisibleBy(n))are identical to the compiler. In both cases,isOddAndNotDivisibleByis never called until something evaluates a result of the expression. In this case, theToArraybetweencandbforces the evaluation. If you added ToArray betweenaandb, you’d get similar times.for comparison you could try: