Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of " += ( s, e )" in the code?

Tags:

c#

What is exactly the += ( s, e ) in the code?

example:

this.currentOperation.Completed += ( s, e ) => this.CurrentOperationChanged();

like image 915
Yanshof Avatar asked Jun 30 '11 08:06

Yanshof


4 Answers

This is the way to attach an event handler using Lambda expression.

For example:

button.Click += new EventHandler(delegate (Object s, EventArgs e) {
            //some code
        });

Can be rewritten using lambda as follows:

button.Click += (s,e) => {
            //some code
        };

One thing to note here. It is not necessary to write 's' and 'e'. You can use any two letters, e.g.

button.Click += (o,r) => {};

The first parameter would represent the object that fired the event and the second would hold data that can be used in the eventhandler.

like image 133
Hasan Fahim Avatar answered Nov 13 '22 11:11

Hasan Fahim


This codes adds an event listener in form of a Lambda expression. s stands for sender and e are the EventArgs. Lambda for

private void Listener(object s, EventArgs e) {

}
like image 31
DanielB Avatar answered Nov 13 '22 12:11

DanielB


This is an assignment of a delegate instance (the start of a lambda expression) to an event invocation list. The s, e represents the sender and EventArgs parameters of the event delegate type.

See http://msdn.microsoft.com/en-us/library/ms366768.aspx for more info.

like image 35
devdigital Avatar answered Nov 13 '22 10:11

devdigital


It is a shorthand for an event handler. s --> object sender and e --> some type of EventArgs.

It can also be rewrriten as:

public void HandlerFunction(object sender, EventArgs e)
{
   this.loaded = true;
}
like image 27
Varun Goel Avatar answered Nov 13 '22 12:11

Varun Goel