我应该使用的EventArgs或简单数据类型? [英] Should I use EventArgs or a simple data type?

查看:162
本文介绍了我应该使用的EventArgs或简单数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在创造的乐趣和实践库,我想知道,引发一个事件时,如何通过自己的EventArgs衍生物或仅仅是数据类型之间进行选择。

例如,在我的图书馆我有这样的事情:

 公共委托无效LostConnectionEventHandler(串地址);
公共委托无效MessageReceieved(byte []的字节数);
 

什么是该标准的做法?我应该替换字符串地址 ConnectionEventArgs 字节[]字节 MessageEventArgs

我知道任何一个工作得很好,这个问题可能是主观的,但我还是好奇的思考过程与否决定,包括自己的EventArgs或只是来传递数据直接在更高级别的程序员去。

谢谢!

解决方案
  

.NET框架的指导方针表明,用于委托类型   事件应采取两个参数,一个对象源参数   指示事件的源,和一个e的参数,它   封装有关该事件的任何其他信息。的类型   在E参数应是来自EventArgs类。对于事件   不使用任何额外的信息,.NET框架具有   已定义一个适当的委托类型:事件处理程序

参考:<一href="http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx">http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

另一个有用的信息:<一href="http://msdn.microsoft.com/en-us/library/ms229011.aspx">http://msdn.microsoft.com/en-us/library/ms229011.aspx

I'm currently creating a library for fun and practice and I was wondering, when raising an event, how to choose between passing your own EventArgs derivative or just the data type.

For example, in my library I have something like this:

public delegate void LostConnectionEventHandler(string address);
public delegate void MessageReceieved(byte[] bytes);

What is the standard practice for this? Should I replace string address with ConnectionEventArgs and byte[] bytes with MessageEventArgs?

I know either one works just fine and this question may be subjective but I am still curious on the thought process higher-level programmers go through when deciding whether or not to include their own EventArgs or just to pass the data in directly.

Thanks!

解决方案

The .NET Framework guidelines indicate that the delegate type used for an event should take two parameters, an "object source" parameter indicating the source of the event, and an "e" parameter that encapsulates any additional information about the event. The type of the "e" parameter should derive from the EventArgs class. For events that do not use any additional information, the .NET Framework has already defined an appropriate delegate type: EventHandler.

Reference: http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Another useful information: http://msdn.microsoft.com/en-us/library/ms229011.aspx

这篇关于我应该使用的EventArgs或简单数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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