简体   繁体   English

代码中的“+=(s,e)”是什么意思?

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

What is exactly the += ( s, e ) in the code?代码中的+= ( s, e )到底是什么?

example:例子:

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

This is the way to attach an event handler using Lambda expression.这是使用 Lambda 表达式附加事件处理程序的方法。

For example:例如:

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

Can be rewritten using lambda as follows:可以使用 lambda 重写如下:

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

One thing to note here.这里要注意一件事。 It is not necessary to write 's' and 'e'.不必写“s”和“e”。 You can use any two letters, eg您可以使用任意两个字母,例如

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.第一个参数将代表触发事件的 object,第二个参数将保存可在事件处理程序中使用的数据。

This codes adds an event listener in form of a Lambda expression.此代码以 Lambda 表达式的形式添加事件侦听器。 s stands for sender and e are the EventArgs . s代表发送者eEventArgs Lambda for Lambda 用于

private void Listener(object s, EventArgs e) {

}

This is an assignment of a delegate instance (the start of a lambda expression) to an event invocation list.这是一个委托实例(lambda 表达式的开始)到事件调用列表的分配。 The s, e represents the sender and EventArgs parameters of the event delegate type. s, e代表事件委托类型的senderEventArgs参数。

See http://msdn.microsoft.com/en-us/library/ms366768.aspx for more info.有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms366768.aspx

It is a shorthand for an event handler.它是事件处理程序的简写。 s --> object sender and e --> some type of EventArgs. s --> object 发送者和 e --> 某种类型的 EventArgs。

It can also be rewrriten as:也可以重写为:

public void HandlerFunction(object sender, EventArgs e)
{
   this.loaded = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM