c# - LINQ extension methods: use Lambda expressions or methods? -


when using linq extension methods enumerable.select, better use lambda expression, or regular method?

i'm asking both respect (memory) optimization*, , readability**.

example code:

private void main() {     var array = new int[1];     var result1 = array.select(x => x.tostring());  // lambda     var result2 = array.select(linqhelper);  // method }  private string linqhelper(int x) {     return x.tostring(); } 


*i'm thinking of closures creating scopes unused instantiated variables in them, because variables in scope when lambda created. edit - stupid thinking, since variables captured closure when they're referenced in lambda expression.
**both options ok me.

from optimization point of view, there should no difference.

from readability point of view, i'd think whether need same logic in several places. if so, use method , use method group conversion. way don't repeat yourself, don't have change several bits of code if requirements change.

if you're using logic in single place , it's short, lambda expression captures logic "inline" in way easier read imo.

i typically avoid long lambda expressions, possible exception of use tpl, e.g.

parallel.foreach(..., x => {    // i'm quite happy have long-ish lambda here - it's foreach    // loop body, basically. }); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -