event EventHandler vs EventHandler

回眸只為那壹抹淺笑 提交于 2021-02-07 13:53:25

问题


In C# Is there a fundamental difference between using

event EventHandler<myeventargs> and EventHandler<myeventargs> As they both produce the same effect from what I can see apart from using the event keyword gives you a different icon in intellisense.


回答1:


They seems to be alike, but really different.

With event keyword, you are making them something like properties, which means you can register them in public, while maintain a private back-end.

However, without event keyword, it's just a public delegate field, and anyone can remove or modify others' events, which is a "encapsulation disaster" as @Jonskeet said.

Check this article by Jon Skeet, it's very helpful :)

Edit:

What I summarized above was not my original thinking, all credits to @Jonskeet's post.



来源:https://stackoverflow.com/questions/25381087/event-eventhandler-vs-eventhandler

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