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.
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
The following article provides an outline for JavaFX EventHandler. In JavaFX, Event Handling is a process which controls an event as well as decides what has to be triggered, when an event occurs. This will be done writing a code called as an event handler that executes when an event occurs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With