" symbol after my method in this C # code mean? I recently asked a question here, and someone provided this answer: private ...">

What does the "=>" symbol after my method in this C # code mean?

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 it => to do? This is the first time I see it.

+5
source share
3 answers

It basically says “I give you this (s,b)” and you bring me back s*bor something else, and if you use lambda with expressions, but it could be something like this: I give you this one (s,b)and do something with them in the operator unit, for example:

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

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

A Lambda - , . - :

delegate int Transformer(int i);

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

}

:

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;}
}

- :

(parameters) => expression or statement-block

x, x*x

x i, x*x int, Transformer;

delegate int Transformer ( int i);

. :

x => {return x*x;}

Expression<T>, lamda . (, " " LINQ)

+10

-. , (s, args) ( ), - , = > .

:

...
client.DownloadStringCompleted += Foo;
}

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

The => is a Lambda Operator . This is a handy little guy who can help make your code more understandable and less cluttered.

+6
source

All Articles