Trey
Trey

Reputation: 63

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.

Upvotes: 6

Views: 1979

Answers (1)

nevets
nevets

Reputation: 4818

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.

Upvotes: 6

Related Questions