36

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 understand the answer What is Expression Lambda?

C# interview Questions

The answer mentioned in that link is this A lambda expression with an expression on the right side is called an expression lambda.

Per my understanding always the expression is in the right hand side only. That is why I am asking this question. Is there anything I am unaware of?

ckv
  • 10,539
  • 20
  • 100
  • 144
  • 4
    Sources for where you encountered both terms would help us explain the difference, if any. – Oded Jun 21 '13 at 17:02
  • 1
    Do you mean the `Expression.Lambda` method call? – Jon Skeet Jun 21 '13 at 17:02
  • Added the source from where i got confused. – ckv Jun 21 '13 at 17:06
  • 3
    Doesn't the source actually show you the difference? – millimoose Jun 21 '13 at 17:07
  • @millimoose: I could not understand that. It just says A lambda expression with an expression on the right side is called an expression lambda. – ckv Jun 21 '13 at 17:09
  • 4
    @ckv It also shows several examples. It also describes a *statement lambda* which is the "opposite". With several examples. (Although the blog sorely needs a code display plugin. And generally looks kind of crappy.) – millimoose Jun 21 '13 at 17:12
  • The question is fun. My personal suggestion is, to understand what is an *expression* and what is a *lambda expression*, then you'll have no problem. – Ken Kin Jun 21 '13 at 17:41

4 Answers4

58

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 2
    Is that all the difference? a statement and statements within blocks. – ckv Jun 21 '13 at 17:12
  • 4
    @ckv When compiled into a `Func`, not really. When compiled into an `Expression>`, yes. For example, some query providers parsing expressions can't handle statement lambdas, or must have statement lambdas in a certain context, etc. In short, the expression tree created is radically different, and whoever is "using" it may care a lot about those details. For just a plain `Func`, the underlying code run when you invoke it is (hopefully) an abstraction you don't need to worry about. – Servy Jun 21 '13 at 17:16
  • @ckv: I don't understand your question. Can you rephrase it? – Eric Lippert Jun 21 '13 at 17:16
  • @Eric I think i got it. I just wanted to know the difference in the 2 usage of the 2 terms: Expression Lambda and Lambda Expression. – ckv Jun 21 '13 at 17:23
  • @ckv It would be better if they had been called "Lambda Expression" (i.e. the whole), and "Expression Lambda Expression" (i.e. the part). As I said before, you should consider better learning material than a terribly formatted anonymous blog. – millimoose Jun 21 '13 at 17:35
  • @millimoose Sure i understand that. I dont have a book ready with me to refer so had to browse to find the answer. – ckv Jun 21 '13 at 17:41
  • 3
    @Servy Except you can't convert a statement lambda into an `Expression`. You *can* create an `Expression` that represents a statement (since .Net 4.0), but you have to use the expression trees API manually for that. – svick Jun 21 '13 at 17:50
  • @svick: I think some of statement lambdas can actually be rewritten as an expression. – Ken Kin Jun 21 '13 at 18:03
  • 1
    How do you compete with a man that responds about C# saying "...we couldn't come up with anything better."? – ChiefTwoPencils Jun 21 '13 at 18:07
  • 1
    @C.Lang: Simple, don't compete; to cooperate. I think to serve people as your will is more constructive. – Ken Kin Jun 21 '13 at 18:39
18

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 }
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • 2
    @millimoose, my English is probably failing me again. The article mentioned by the questioner differentiates expression lambdas (versus statement lambdas) as lambdas whose bodies only consist in a single expression. IIRC "expression" lambdas can be assigned to `Expression>` but "statement" lambdas cannot. – Frédéric Hamidi Jun 21 '13 at 17:08
  • 3
    @FrédéricHamidi Correct. And statement lambdas and expression lambdas are the two different types of lambda expressions. A lambda is either wrapping a statement or an expression. The resulting lambda is itself an expression (regardless of which of the two it is wrapping). – Servy Jun 21 '13 at 17:13
  • @FrédéricHamidi Thanks for answering even though I was being overly snippy there. "Whose bodies consist of" is a better phrasing, I was confused by "returns an expression" since I think of a lambda as returning the result of whatever the body is. – millimoose Jun 21 '13 at 17:31
  • @millimoose, that's why I actually said `evaluates an expression`, but I guess that was even more confusing ;) – Frédéric Hamidi Jun 21 '13 at 17:40
10

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; }
svick
  • 236,525
  • 50
  • 385
  • 514
1

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.

svick
  • 236,525
  • 50
  • 385
  • 514
Tormod
  • 4,551
  • 2
  • 28
  • 50