c# - Is there a reason not to use Func for one off functions? -


is there reason should do

public class foo {   int bar() {     return 1;   }   public void baz() {      int bar = bar();      // other stuff   } } 

vs

public class foo {   public void baz() {     func<int> bar = ()=> {       return 1;     };     int bar = bar();     // other stuff   } } 

i latter in order show intent bar exists solely execution baz, don't want if there problems it.

perfectly vaild, smart looking, never in code used somone else. if , whoever else looking @ code used style see no problems it.

pro:

  • clearly shows scope
  • very functional looking

con:

  • function becomes longer (especially if don't use k&r curly braces style)
  • very non-traditional c# code. inline lambdas used everywhere now, explicit functions not used.
  • could unintentionally capture state outer function.
  • likley small performance hit , unlikley inlined jit.

Comments