What does this code mean (s, e)?
Asked Answered
D

7

8

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 systems http://msdn.microsoft.com/en-us/library/dd833066(v=VS.95).aspx

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

Demisemiquaver answered 1/9, 2011 at 13:24 Comment(0)
H
6

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.

Hindemith answered 1/9, 2011 at 13:30 Comment(1)
Thank you all for your help with this! I've ticked this one because it convert the confusing to something I understand but I do appreciate the other comments that have improved my understanding.Demisemiquaver
U
9

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

Untrue answered 1/9, 2011 at 13:26 Comment(0)
Q
9

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();
}
Quiescent answered 1/9, 2011 at 13:28 Comment(1)
+1 This answer gets my vote. Sets the appropriate pattern for a good stackoverflow answer IMO.Piton
H
6

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.

Hindemith answered 1/9, 2011 at 13:30 Comment(1)
Thank you all for your help with this! I've ticked this one because it convert the confusing to something I understand but I do appreciate the other comments that have improved my understanding.Demisemiquaver
L
2

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);
}
Laborsaving answered 1/9, 2011 at 13:28 Comment(0)
E
1

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.

Ecto answered 1/9, 2011 at 13:27 Comment(0)
S
1

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) =>.

Sisterhood answered 1/9, 2011 at 13:31 Comment(0)
C
1

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.

Chiffonier answered 1/9, 2011 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.