Custom EventHandler vs. EventHandler<EventArgs>

杀马特。学长 韩版系。学妹 提交于 2019-12-02 15:21:47

You're right; they do the same thing. Thus, you should probably prefer the former over the latter because it's clearer and requires less typing.

The reason that lots of the .NET Framework classes have their own custom event handler delegates is because they were written before generics (which allowed the shorthand syntax) were introduced in version 2.0. For example, almost all of the WinForms libraries were written before generics, and back in those days, the latter form was the only way of doing things.

The second way gives more flexibility and type safety. There are less methods with corresponding signature => less place for a mistake. Custom delegate allows to specify exactly parameters you need (or specify no one) - no sender+args cargo cult.

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