Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between expression lambda and statement lambda

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?

like image 847
ckv Avatar asked Jun 21 '13 17:06

ckv


People also ask

What is statement lambda?

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)

Can lambda expressions contain statements?

In particular, a lambda function has the following characteristics: It can only contain expressions and can't include statements in its body. It is written as a single line of execution.

What is the difference between lambda expression and LINQ?

Language Integrated Query (LINQ) is feature of Visual Studio that gives you the capabilities yo query on the language syntax of C#, so you will get SQL kind of queries. And Lambda expression is an anonymous function and is more of a like delegate type.


1 Answers

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.

like image 152
Eric Lippert Avatar answered Sep 30 '22 19:09

Eric Lippert