简体   繁体   English

C# Lambda 表达式语法:是否需要括号?

[英]C# Lambda expression syntax: are brackets necessary?

I'm new in C# and earlier I saw the lambda expression is like我是 C# 新手,早些时候我看到 lambda 表达式就像

(params) => { expression; }

but in LINQ, I saw examples like但在 LINQ 中,我看到了这样的例子

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.非常感谢。

The rules are:规则是:

A lambda expression has the form lambda 表达式具有以下形式

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

Let's consider the left side first.让我们首先考虑左侧。

The modifier can be ref, out or nothing at all.修饰符可以是 ref、out 或什么都没有。

If there are no ref or out modifiers then all the types can be elided.如果没有 ref 或 out 修饰符,则可以省略所有类型。 If there are any ref or out modifiers then every parameter declaration must have a declared type.如果有任何ref 或 out 修饰符,则每个参数声明都必须具有声明的类型。 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.因此,您可以省略类型,前提是 (1) 没有 refs 或 outs,以及 (2) 省略所有类型 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:如果语句列表由带有表达式的单个 return 语句组成:

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.现在 lambdas 将被读取为返回它们。

Note that this means it is legal to do this trick with void returning methods .请注意,这意味着使用 void 返回方法执行此技巧是合法的 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: { }允许你在 lambda 表达式的主体中放置一个语句块:

(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 :没有它们,lambda 表达式的主体就是一个表达式

(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:像 ReSharper 这样的 Visual Studio 插件会指出这样的冗余:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM