简体   繁体   中英

event EventHandler vs EventHandler

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM