2011-09-29 182 views
6

=>是什么意思?这里的一个代码卡扣:'=>'是什么意思?

Dispatcher.BeginInvoke((Action)(() => { trace.Add(response); })); 
+0

此外,您正在查看的示例使得很难理解Lambda运算符的作用。请参阅下面的一些示例以及下面的链接。 –

+0

不要忘记标记答案为接受,如果你有你想要的信息' –

+0

我认为这个问题会引发很多有类似含义的答案。 –

回答

7

它的lambda表达式是匿名委托的简化的语法。它读取'去'。相当于Dispatcher.BeginInvoke((Action)delegate() { trace.Add(response); });

2

=>是λ表达式运算符,它表明该代码是λ表达式。

(param) => expr(int x) = > { return x + 1 }; 

param => exprx=> x + 1;> 

什么是Lambda表达式?

* Lambda expression is replacement of the anonymous method avilable in C#2.0 Lambda 
    expression can do all thing which can be done by anonymous method. 
* Lambda expression are sort and function consist of single line or block of statement. 

了解更多:Lambda Expressions

+1

“lambda”中有一个“b” – phoog

0

它是λ操作者读取像“变为”

0

这种“=>”是指在C#中使用lambda表达式语法。

此语法自.NET 3.5(C#3.0)中的Visual Studio 2008起可用。这是MSDN official documentation of lambda expression in C#

上面的代码是一样的匿名委托已经可用,因为C#2.0

您的代码:

Dispatcher.BeginInvoke((Action)(() => { trace.Add(response); })); 

被翻译成:

Dispatcher.BeginInvoke(new delegate() { trace.Add(response); }); 

这两个代码基本上具有相同的语义。

0

值得注意的是,单个表达式lambda不需要{}在身体周围,也不需要分号,因此您可以简化代码(略)。

Dispatcher.BeginInvoke((Action)(() => trace.Add(response)));