Why to not use a custom class instead of inheriting the EventArgs class

旧城冷巷雨未停 提交于 2019-12-17 19:37:22

问题


I'm wondering why should I use a class that inherits from the EventArgs class instead of using a custom class that will do the same job for me when passing event data?


回答1:


You don't have to inherit from EventArgs, but it allows people using your classes to use and handle generic *Handler(object sender, EventArgs e) declarations. If you don't inherit from EventArgs, then they have to use explicitly typed

*Handler(object sender, YairsFakeEventArgs e)

The same goes for just using custom delegates but they are a lot more explicitly different.




回答2:


Because your event-args class will be compatible with any other function that accepts an EventArgs object.




回答3:


Because that's what would be the standard and most idiomatic way to do this in .NET. The whole ASP.NET and WinForms event model relies on those conventions. If another developer reads your code he will more easily understand it as this is the standard. This being said you could of course use any class you like.



来源:https://stackoverflow.com/questions/6816848/why-to-not-use-a-custom-class-instead-of-inheriting-the-eventargs-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!