Using a lambda expression versus a private method

前端 未结 7 1136
北海茫月
北海茫月 2021-02-02 13:42

I read an answer to a question on Stack Overflow that contained the following suggested code:

Action logAndEat = ex => 
{  
    // Log Error          


        
7条回答
  •  执笔经年
    2021-02-02 14:08

    I guess you could think of the lambda expression in this example as being a little bit like Pascal's nested functions, in that it's code that can only be executed by the method it's declared in.

    A private method could be called from any method in the same class, while a lambda like this is local to the current method and is therefore explicitly only used in that context.

    That's really the only advantage I can think of - being explicit in terms of expected usage.

提交回复
热议问题