事件处理程序和delegete无效之差() [英] Difference between EventHandler and delegete void()

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

问题描述

结果
我只是好奇,究竟是什么简单evenhandler不错。
我可以有:


I am just curious what exactly is simple evenhandler good for. I can have:

event EventHandler blah;

delegate void Blah();
event Blah Blah1;



有没有用,除了不放过一个额外的代码行事件处理器的一些优势?谢谢你。

Is there some advantage of using EventHandlers except sparing an extra line of code? Thanks.

推荐答案

这是事件处理程序代表一对夫妇额外的参数。第一个是发送者,即,导致该事件的对象,第二个是额外事件数据。它只是宣告事件是很有用的一贯模式。没有通过发件人作为参数,你不能轻易检测哪些对象造成它,这可能会导致不必要的重复(复制和粘贴)代码。如果按照标准的事件模式,事件处理程序可以进行基于引发事件的对象的属性不同的任务。

An EventHandler is a delegate with a couple extra parameters. The first one is the sender, that is, the object that caused that event and the second is extra event data. It's simply a consistent pattern useful for declaring events. Without passing the sender as an argument, you cannot easily detect which object caused it and this might result in unnecessarily duplicated (copy-and-pasted) code. If you follow the standard event pattern, your event handler can carry on different tasks based on a property of the object that caused the event.

此外,一些事件进行额外的信息沿着它们,例如事件类型 MouseEventHandler 将通过在其第二个参数,鼠标指针的位置( MouseEventArgs )对于事件处理程序消耗。这个模式的好处是,你可以忽视的辅助数据,如果你的事件处理程序一般并不需要它。您可以挂钩一个方法事件处理程序签名到 MouseEventHandler 事件为例(因为的类型第二个参数从 EventArgs的)。

Besides, some events carry extra information along them, for example an event typed MouseEventHandler will pass the location of the mouse pointer in its second argument (MouseEventArgs) for your event handler to consume. The good thing about this pattern is that you can ignore the auxiliary data if your event handler is general and doesn't need it. You are able to hook up a method with EventHandler signature to a MouseEventHandler event, for example (since the type of the second argument inherits from EventArgs).

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

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