Expression Lambda versus Statement Lambda

前端 未结 8 904
北荒
北荒 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:33

    Same for the OP example, but after C# 6.0, allowing you to use same expression syntax to define normal non-lambda methods within a class. For example:

    public static double AreaOfTriangle(double itsbase, double itsheight)
    {
        return itsbase * itsheight / 2;
    }
    

    The above code snippet can be written only if the method can be turned into a single expression. In short, it can be used the expression lambda syntax, but not the statement lambda syntax.

    public static double 
                  AreaOfTrianglex(double itsbase, double itsheight) => itsbase * itsheight / 2;
    

提交回复
热议问题