加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我应该使用EventArgs还是简单的数据类型?

发布时间:2020-12-15 18:18:09 所属栏目:百科 来源:网络整理
导读:我正在创建一个有趣和实践的库,我想知道,在举办活动时,如何选择传递自己的EventArgs衍生物或仅仅是数据类型. 例如,在我的库中我有这样的东西: public delegate void LostConnectionEventHandler(string address);public delegate void MessageReceieved(byt
我正在创建一个有趣和实践的库,我想知道,在举办活动时,如何选择传递自己的EventArgs衍生物或仅仅是数据类型.

例如,在我的库中我有这样的东西:

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

这是什么标准做法?我应该用MessageEventArgs替换字符串地址和用MessageEventArgs替换byte []字节吗?

我知道其中任何一个工作得很好而且这个问题可能是主观的但我仍然对高级程序员在决定是否包含他们自己的EventArgs或仅直接传递数据时所经历的思考过程感到好奇.

谢谢!

解决方法

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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读