什么是符号QUOT; = GT;"我在这个C#代码方法后做什么?
最近,我在这里问一个问题,有人提供了这样的回答:
I recently asked a question here, and someone provided this answer:
private void button1_Click(object sender, EventArgs e)
{
var client = new WebClient();
Uri X = new Uri("http://www.google.com");
client.DownloadStringCompleted += (s, args) => //THIS, WHAT IS IT DOING?
{
if (args.Error == null && !args.Cancelled)
{
MessageBox.Show();
}
};
client.DownloadStringAsync(X);
}
什么是=>在做什么?这是我第一次看到这一点。
What is that => doing? It's the first time I'm seeing this.
基本上它说:我给你这个 (S,b)
,你正在返回取值* b
我什么,如果你正在使用的lambda用表情,但它可以是东西是这样的:我给你这个(S,b)
并与他们做的东西像语句块:
Basically it says "I am giving you this (s,b)
" and you are returning me s*b
or something and if you are using lambda with expressions, but it can be something like this : I am giving you this (s,b)
and do something with them in the statement block like :
{
int k = a+b;
k = Math.Blah(k);
return k;
}
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
一个 LAMBDA
表情写在地方委托实例的一位不愿透露姓名的方法。编译器立即lambda表达式转换为两种:
A Lambda
expression is an unnamed method written in place of a delegate instance. The compiler immediately converts the lambda expression to either :
- 一个委托实例
- 的表达式树
委托INT变压器(int i)以
class Test{
static void Main(){
Transformer square = x => x*x;
Console.WriteLine(square(3));
}
}
我们可以重写它像这样:
We could rewrite it like this :
委托INT变压器(int i)以
class Test{
static void Main(){
Transformer square = Square;
Console.WriteLine(square(3));
}
static int Square (int x) {return x*x;}
}
一个lambda表达式有以下形式:
A lambda expression has the following form :
(参数)=>表达式或语句块
在前面的例子中有一个参数, X
,而表达的是 X * X
In previous example there was a single parameter,x
, and the expression is x*x
在我们的例子中,x对应参数i,表达式 X * X
对应的返回类型 INT
,因此是与变压器的委托兼容;
in our example, x corresponds to parameter i, and the expression x*x
corresponds to the return type int
, therefore being compatible with Transformer delegate;
委托INT变压器(int i)以
一个lambda表达式的代码可以是一个语句块而不是表达。我们可以重写我们的例子如下:
A lambda expression's code can be a statement block instead of an expression. We can rewrite our example as follows :
x => {return x*x;}
这是表达式树,类型表达式来; T>
,表示在穿越的对象模型的LAMDA表达式中的代码。这允许lambda表达式稍后在运行时(请检查LINQ查询表达式)intrepreted
An expression tree, of type Expression<T>
, representing the code inside the lamda expression in a traversable object model. This allows the lambda expression to be intrepreted later at runtime (Please check the "Query Expression" for LINQ)