开发者

What does this code mean (s, e)?

How does this code work?

      app.InstallStateChanged += (s, e) => UpdateUI();
      NetworkChange.NetworkAddressChanged +=
            (s, e) => UpdateNetworkIndicator();

Can someone please unscramble this?

The code comes from an example used in a silverlight 4 OOB sy开发者_Go百科stems http://msdn.microsoft.com/en-us/library/dd833066(v=VS.95).aspx

UpdateNetworkIndicator does not return anything. UpdateUI does not return anything.


This is a lambda expression that contains multiple parameters. In this case (as you are using the function to replace an event handler) they are equivalent to the object and EventArgs parameters.

Your code is equivalent to the below

app.InstallStateChanged += OnInstallStateChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;

/* ... */

private void OnInstallStateChanged(object s, EventArgs e)
{
    UpdateUI();
}

private void OnNetworkAddressChanged(object s, EventArgs e)
{
    UpdateNetworkIndicator();
}


That's a lambda expression.

"All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."

http://msdn.microsoft.com/en-us/library/bb397687.aspx


Both the UpdateUI() and UpdateNetworkIndicator() methods are custom event handler methods.

The += operator is attaching these event handlers to the events fired by the app and NetworkChange respectively.

The => denotes a lambda expression. The (s,e) are input parameters (in this case, the standard sender, event args) and the right of => is the statement or expression.

In this case, you could rewrite this as:

app.InstallStateChanged += UpdateUI;
NetworkChange.NetworkAddressChanged += UpdateNetworkIndicator;

and it should work just as well.


Consider this example

Button1.Click += (s, e) => log(e);

is the short hand (using lambda expression) for

Button1.Click += new EventHandler(Button1_Click);

and

void Button1_Click(object sender, EventArgs e)
{
    log(e);
}


InstallStateChanged and NetworkAddressChanged are Events, what you´re seeing is the lambda syntax to define eventhandler to call UpdateUI or UpdateNetworkIndicator if the respective events are fired.


That syntax is called Lambda Expression. From MSDN,

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

And the (s, e) declare input parameters of created delegate or expression tree. When there's single input parameter, parentheses are not needed and you can write just s => .... When there's more than one parameter, parentheses are required - (s, e) =>.


In short, the s and e refer to objects whose properties the function is targeting. The code is expressing: you have two types of objects you can access in this function. When this function is invoked, from the s object get this or that property and use its value in the following way.

So in a hypothetical context

S is a string E is an int

(s, e) =>

Means that further references in the function are referring to those respective object types by the names s and e.

(s , e) => s.Length + e

Means: get the value of the instance of a string to which s refers and add to it the value of the instance of an int to which e refers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜