Expression Lambda versus Statement Lambda

前端 未结 8 926
北荒
北荒 2020-12-30 02:02

Fundamentally, is there any difference between a single-line expression lambda and a statement lambda? Take the following code, for example:

private delegate         


        
8条回答
  •  青春惊慌失措
    2020-12-30 02:19

    No, there is no difference in this example. If the body of the lambda is only one expression, you can drop the brackets. However, once the lambda contains more than one expression, like so:

    MyDelegate myDelegate2 = () => { 
      Console.WriteLine("Test 2");                      
      Console.WriteLine("Test 2"); 
    };
    

    the brackets are mandatory.

提交回复
热议问题