开发者

what is += (o, arg) => actually achieving?

开发者 https://www.devze.com 2023-03-22 21:18 出处:网络
Sorry to ask all, but I\'m an old hand Vb.net guy who\'s transferring to c#.I havethe following piece of code that seems to activate when the (in this case) postAsync method is fired.I just don;t unde

Sorry to ask all, but I'm an old hand Vb.net guy who's transferring to c#. I have the following piece of code that seems to activate when the (in this case) postAsync method is fired. I just don;t understand what the code is doing (as follows):-

app.PostCompleted +=
    (o, args) =>
    {
        if (args.Err开发者_开发问答or == null)
        {
            MessageBox.Show("Picture posted to wall successfully.");
        }
        else
        {
            MessageBox.Show(args.Error.Message);
        }
    };

if anyone could explain what the += (o,args) => is actually acheiving I'd be so greatful....

many thanks in advance. Tim


(o,args) => defines a lambda expression that takes two parameters named o and args. The types of those parameters is inferred according to the type of PostCompleted (if PostCompleted is an EventHandler, then they will be respectively of type Object and EventArgs). The expression's body then follows after the =>.

The result is than added as an handler to PostCompleted.

As such, it's a less verbose way to write:

app.PostCompleted += delegate(object o, EventArgs args)
{
    // ...
};

Which is a shorthand for:

void YourHandler(object o, EventArgs args)
{
    // ...
}

// ...

app.PostCompleted += YourHandler;


That is an added handler for the PostCompleted event using a lambda expression. It is similar to

  app.PostCompleted += MyHandler;

  // ...

  private void MyHandler(object sender, EventArgs e) {
      // ...
  }

But when using lambda expressions, you can't detach the handler easily.


It's shorthand for a delegate defining the event handler for the POST completion event:

app.PostCompleted += delegate(object o, EventArgs args) { 
    // ...
};

See also Anonymous Methods.


Assuming PostCompleted is an event, you are basically creating an event handler using lambda notation.

This code snipped is equivalent to:

app.PostCompleted += delegate (o, args)
{
    if (args.Error == null)
    {
        MessageBox.Show("Picture posted to wall successfully.");
    }
    else
    {
        MessageBox.Show(args.Error.Message);
    }
};


The vb.net equivalent would look like this:

AddHandler app.PostCompleted, Function(o, args)
        ''# ...
    End Function

Note that this requires Visual Studio 2010/.Net 4, but the C# code works back in Visual Studio 2008/.Net 3.5.

But that's only partly true. In C#, this is a way to define a method as an event handler in one place. In VB.Net, you can use the Handles keyword, and so the actual equivalent might look more like this:

Public Sub App_PostCompleted(ByVal Sender as Object, ByVall e As EventArgs) Handles app.PostCompleted
    ''#
End Sub

But even that's not completely equivalent, since you gave the method a name and can call it from anywhere. The only reference to the C# code (and thus the only way to call it) is through the event subscription.


The (o,args) => part is a lambda expression, which is an anonymous function.

the += part assigns the lambda expression to be called when the event fires.

0

精彩评论

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