2011-09-29 183 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)));