Difference between expression lambda and statement lambda

后端 未结 4 1358
长情又很酷
长情又很酷 2021-02-01 03:38

Is there a difference between expression lambda and statement lambda?

If so, what is the difference?

Found this question in the below link but could not understa

相关标签:
4条回答
  • 2021-02-01 04:16

    This is indeed confusing jargon; we couldn't come up with anything better.

    A lambda expression is the catch-all term for any of these:

    x => M(x)
    (x, y) => M(x, y)
    (int x, int y) => M(x, y)
    x => { return M(x); }
    (x, y) => { return M(x, y); }
    (int x, int y) => { return M(x, y); }
    

    The first three are expression lambdas because the right hand side of the lambda operator is an expression. The last three are statement lambdas because the right hand side of the lambda operator is a block.

    This also illustrates that there are three possible syntaxes for the left side: either a single parameter name, or a parenthesized list of untyped parameters, or a parenthesized list of typed parameters.

    0 讨论(0)
  • 2021-02-01 04:19

    lampda expression is an anonymous function that the compiler can either turn into a Func<T> or an Expression<Func<T>> (inferred by compiler depending on usage).

    It is not entirely clear what you mean by "expression lamdba", but if you have heard the phrase in a podcast/webcast or something it is probably either referring to a lambda expression. Or it could be the property Expression.Lambda, which you use to obtain a lambda from an Expression instance.

    0 讨论(0)
  • 2021-02-01 04:31

    A lambda expression is a syntax that allows you to create a function without name directly inside your code, as an expression.

    There are two kinds of lambda expressions, depending on their body:

    • expression lambdas, whose body is just an expression, e.g. (i, j) => i + j
    • statement lambdas, whose body is a full statement, e.g.. (i, j) => { return i + j; }
    0 讨论(0)
  • 2021-02-01 04:33

    Yes there is - or I should probably say that one defines the other.

    A lambda expression allows you to assign simple anonymous functions.

    An expression lambda is a type of lambda that has an expression to the right of the lambda operator.

    The other type of lambda expression is a statement lambda because it contains a statement block {...} to the right side of the expression.

    • Expression lambda takes the form: number => (number % 2 == 0)
    • Statement lambda takes the form: number => { return number > 5 }
    0 讨论(0)
提交回复
热议问题