删除事件处理程序 [英] Removing event handlers
问题描述
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屋!