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

前端 未结 4 1070
无人及你
无人及你 2021-02-06 11:22

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

example:

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

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 11:45

    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.

提交回复
热议问题