What's the point in the standard pattern for event delegates in .net? I.e. the EventHandler predefined delegate? Why not just choose the most appropriate signature for the requirement?
Edit: And furthermore, is it necessary to slavishly follow Microsoft's example in all cases?
In C# 1.0 the convention didn't make a lot of sense. In C# 2.0, where there is a certain amount of delegate variance, it makes more sense.
If you have an event handling method which doesn't really care about the parameters, e.g.
public void SaveDocument(object sender, EventArgs args)
{
// Save the document here
}
you can now use that method to subscribe to any event which follows the convention, even if the actual sender or args are more strictly typed. This means you can use the same handler for a keyboard event, a button click etc.
It's mainly for consistency and versioning—with the sender
paramater as always System.Object and the args
parameter as always a System.EventArgs, or a dervied class thereof, you can change the sender argument passed to it as well as change the EventArgs type afterwards without breaking any existing code.
The whole .NET eventing idiom was designed to support component-based development, where the calling code is not under control of the developer of the component with the events.
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