删除事件处理程序 [英] Removing event handlers

查看:104
本文介绍了删除事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是:

  Button.Click  -  = new EventHandler(Button_Click); 

与此相同:

  Button.Click  -  = Button_Click; 

我问,因为对我来说,似乎前者正在删除对方法的新引用,后一种方法本身就是去除一个方法。但是,再一次,也许新的EventHandler部分是隐含在+ =或 - =重载的情况下,程序员没有明确赋值呢?



如果不同于

  Button.Click  -  = new EventHandler(Button_Click); 

VS

  Button.Click  -  = Button_Click; 

谢谢。

解决方案

这是一样的。第二个仅仅是第一个的句法糖,而对于代表类型,相等的比较被适当地重载:


两个同样类型的代理与相同的目标,方法和调用列表被认为是相等的。


资料来源: MSDN,Delegate.Equality Operator


Is this:

Button.Click -= new EventHandler(Button_Click);

the same as this:

Button.Click -= Button_Click;

I ask because to me it seems that the former is removing a new reference to a method, and the latter one is removing a method itself. But then again, maybe the new EventHandler part is implicit in the += or -= overload in case the programmer doesn't explicitly assign it like that?

In case it is different how about

Button.Click -= new EventHandler(Button_Click);

VS

Button.Click -= Button_Click;

Thanks.

解决方案

It is the same. The second is merely syntactic sugar for the first, and equality comparison is overloaded appropriately for delegate types:

Two delegates of the same type with the same targets, methods, and invocation lists are considered equal.

Source: MSDN, Delegate.Equality Operator

这篇关于删除事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆