C# Lambda expression syntax: are brackets necessary?

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.


Which brackets are you talking about? ( ) or { }?


( ) are used in the parameter list and are required when you have more than one parameter:

(a, b, c) => ...

You can omit them when you have only one argument:

a => ...

{ } allow you to put a block of statements in the body of lambda expressions:

(a, b, c) => {
                 Console.WriteLine("Hello World!");
                 Console.WriteLine("a = {0}", a);
                 Console.WriteLine("b = {0}", b);
                 Console.WriteLine("c = {0}", c);
                 return a * b + c;
             }

Without them, the body of a lambda expression is an expression:

(a, b, c) => a * b + c

You only need brackets if you have multiple parameters.

Update (As is customary on SO when answers are edited after other people have answered...)

Using brackets (parenthesis "( )" and braces "{ }") is this case are redundant. The statements are equivalent. A Visual Studio add-in like ReSharper will point redundancies like this out:

http://www.jetbrains.com/resharper/

Tags:

C#

Lambda

Syntax