Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is generic EventHandler<TArgs> so under-used?

.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.

like image 854
Bradley Smith Avatar asked Nov 12 '10 06:11

Bradley Smith


2 Answers

Just an accident of history. If we had generics in .NET 1 most of the other delegates wouldn't exist.

like image 144
Jonathan Allen Avatar answered Oct 21 '22 01:10

Jonathan Allen


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.

like image 25
Jon Skeet Avatar answered Oct 21 '22 01:10

Jon Skeet