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

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

问题描述

是这样的:

Button.Click -= new EventHandler(Button_Click);

此相同

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?

在情况下,它是关于

Button.Click -= new EventHandler(Button_Click);

VS

Button.Click -= Button_Click;

感谢。

推荐答案

这是一样的。第二个是第一个只是语法糖,和平等比较适当重载委托类型:

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.

来源: MSDN,Delegate.Equality操作

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

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