.NET 2.0 added the EventHandler<TArgs>
generic delegate type to simplify the process of writing custom events; instead of having to define an EventArgs
class and its corresponding delegate (e.g. MyEventArgs
and MyEventHandler
), you need only write the args class.
Bearing that in mind, why does this delegate type appear almost nowhere in the .NET Framework? I know that most of the core APIs were developed before generics were introduced, but even in new parts of the framework like WPF, they have opted for explicitly defining delegate types; e.g. RoutedEventHandler
instead of EventHandler<RoutedEventArgs>
.
Is there something inherently wrong with the generic event handler delegate? I often make use of it and I worry that my code appears very out of place when compared to built-in classes.
Just an accident of history. If we had generics in .NET 1 most of the other delegates wouldn't exist.
I don't think there's anything wrong with it. It is used in some places of the framework... in the GeoCoordinateWatcher.Position
event, just as a random example.
It's a little clumsier to see in code than a specific type name like RoutedEventHandler
- and as you're going to use RoutedEventHandler
a lot in WPF/Silverlight, maybe that's why MS decided to give it its own type.
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