Difference between expression lambda and statement lambda

≡放荡痞女 提交于 2019-12-02 19:09:34

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.

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 }

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; }
Tormod

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!