开发者

What does '=>' mean?

开发者 https://www.devze.com 2023-04-08 23:34 出处:网络
What does => means? Here\'s a code snap: Dispatch开发者_如何学运维er.BeginInvoke((Action)(() => { trace.Add(response); }));

What does => means? Here's a code snap:

Dispatch开发者_如何学运维er.BeginInvoke((Action)(() => { trace.Add(response); }));


it's lambda expression which is the simplified syntax of anonymous delegate. it reads 'goes to'. equivalent to Dispatcher.BeginInvoke((Action)delegate() { trace.Add(response); });


=> is lambda expression operator which indicate that the code is lambda expression.

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

or

param => exprx=> x + 1;>

What is Lambda expression ?

* 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.

Read more : Lambda Expressions


=> is an operator called Lambda Operator

It is used for creating a lambda expression


It's the lambda operator =>


It is a lambda operator which reads like "goes to"


This "=>" means the use of lambda expression syntax in C#.

This syntax is available since Visual Studio 2008 in .NET 3.5 (C# 3.0). This is the MSDN official documentation of lambda expression in C#.

The code above is the same as anonymous delegate in already available since C# 2.0

Your code:

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

is translated into:

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

Those two codes essentially have the same semantics.


It's worth noting that a single expression lambda doesn't need the {} around the body, nor does it need a semicolon, so you can simplify your code (slightly) to.

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

精彩评论

暂无评论...
验证码 换一张
取 消