简体   繁体   English

在这个C#代码中我的方法之后做什么符号“=>”?

[英]What is the symbol “=>” doing after my method in this C# code?

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. 这是我第一次看到这个。

That is the lambda operator. 那是lambda运算符。 You define an anonymous function which takes two arguments (s, args) (type specifiers omitted), and the body of said function is what appears after the => symbol. 你定义了一个匿名函数,它接受两个参数(s,args)(省略了类型说明符),并且所述函数的主体是在=>符号后出现的。

It is conceptually the same as this: 它在概念上与此相同:

...
client.DownloadStringCompleted += Foo;
}

void Foo(object sender, EventArgs args)
{
    if (args.Error == null && !args.Cancelled)
    {
        MessageBox.Show();
    }
};

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 : 基本上它说“我给你这个(s,b) ”并且你正在给我回复s*b或者其他什么东西如果你正在使用带有表达式的lambda,但它可能是这样的:我给你这个(s,b)并在语句块中对它们执行某些操作,如:

{
  int k = a+b;
  k = Math.Blah(k);
  return k;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A Lambda expression is an unnamed method written in place of a delegate instance. Lambda表达式是代替委托实例编写的未命名方法。 The compiler immediately converts the lambda expression to either : 编译器立即将lambda表达式转换为:

  • A delegate instance 委托实例
  • An expression Tree 表达式树

delegate int Transformer(int i);

class Test{
  static void Main(){
     Transformer square = x => x*x;
     Console.WriteLine(square(3));
  } 

}

We could rewrite it like this : 我们可以像这样重写它:

delegate int Transformer(int i);

class Test{
  static void Main(){
     Transformer square = Square;
     Console.WriteLine(square(3));
  } 
  static int Square (int x) {return x*x;}
}

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

(parameters) => expression or statement-block

In previous example there was a single parameter, x , and the expression is x*x 在前面的例子中,有一个参数x ,表达式是x*x

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; 在我们的例子中,x对应于参数i,表达式x*x对应于返回类型int ,因此与Transformer委托兼容;

delegate int Transformer ( int i);

A lambda expression's code can be a statement block instead of an expression. lambda表达式的代码可以是语句块而不是表达式。 We can rewrite our example as follows : 我们可以重写我们的例子如下:

x => {return x*x;}

An expression tree, of type Expression<T> , representing the code inside the lamda expression in a traversable object model. Expression<T>类型的表达式树,表示可遍历对象模型中lamda表达式内的代码。 This allows the lambda expression to be intrepreted later at runtime (Please check the "Query Expression" for LINQ) 这允许lambda表达式稍后在运行时进行编译(请检查LINQ的“查询表达式”)

The => is the Lambda Operator . =>是Lambda运算符 It's a handy little guy that can help make your code more readable and less cluttered. 这是一个方便的小家伙,可以帮助您的代码更具可读性,更少杂乱。

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

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