Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Lambda expression syntax: are brackets necessary?

Tags:

syntax

c#

lambda

I'm new in C# and earlier I saw the lambda expression is like

(params) => { expression; }

but in LINQ, I saw examples like

IEnumerable<string> customerFirstNames = customers.Select(cust => cust.FirstName);

No brackets.

(I actually mean both {} and () - regardless if we call them braces, parenthesis, or brackets.)

Are they the same or is there any difference?

Thanks a lot.

like image 332
LLS Avatar asked Jun 05 '10 13:06

LLS


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

The rules are:

A lambda expression has the form

( modifier type parameter, modifier type parameter ...) => { statements }

Let's consider the left side first.

The modifier can be ref, out or nothing at all.

If there are no ref or out modifiers then all the types can be elided. If there are any ref or out modifiers then every parameter declaration must have a declared type. If any paramter has a declared type then every parameter must have a declared type. So you can elide the types provided that (1) there are no refs or outs, and (2) you elide all of them. Otherwise, you must provide all the types.

If there is exactly one parameter and its type has been elided then the parentheses around the parameter list may optionally be elided also.

That's all the rules about parameter lists. The rules about the right side are:

if the statement list consists of a single return statement with an expression:

x => { return x + 1; }

then the braces, return keyword and semicolon may be elided:

x => x + 1

furthermore, if the statement list consists of a single statement that is a statement expression:

x => { x++; } // Not returning the value of x++; only useful for the side effects
x => { new Y(x); } // weird! executing a ctor only for its side effects! But legal!
x => { M(x); } // note, not returning the value of M(x) even if there is one.

then it is also legal to elide the braces and semicolon:

x => x++
x => new Y(x)  
x => M(x)

Note that these now potentially mean something different to the reader! Before we were clearly discarding the return values; now the lambdas will be read as returning them.

Note that this means it is legal to do this trick with void returning methods. This is actually legal:

x => Console.WriteLine(x)

Yuck. Don't do that. If you mean

x => { Console.WriteLine(x); } 

then say that instead. The former looks too much like you are trying to say

x => { return Console.WriteLine(x); }

which of course would be illegal.

like image 136
Eric Lippert Avatar answered Sep 22 '22 13:09

Eric Lippert