I am just curious what exactly is simple evenhandler good for. I can have:
event EventHandler blah;
or
delegate void Blah();
event Blah Blah1;
Is there some advantage of using EventHandlers except sparing an extra line of code?
An EventHandler is a delegate with a couple extra parameters. The first one is the sender, that is, the object that caused that event and the second is extra event data. It's simply a consistent pattern useful for declaring events. Without passing the sender as an argument, you cannot easily detect which object caused it and this might result in unnecessarily duplicated (copy-and-pasted) code. If you follow the standard event pattern, your event handler can carry on different tasks based on a property of the object that caused the event.
Besides, some events carry extra information along them, for example an event typed MouseEventHandler will pass the location of the mouse pointer in its second argument (MouseEventArgs) for your event handler to consume. The good thing about this pattern is that you can ignore the auxiliary data if your event handler is general and doesn't need it. You are able to hook up a method with EventHandler signature to a MouseEventHandler event, for example (since the type of the second argument inherits from EventArgs).
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