Assuming we declare a class DerivedEventArgs:
public class DerivedEventArgs : EventArgs { ... }
then EventHandler delegate is able to accept methods with the following signature:
public static void Some_Method(object o, DerivedEventArgs e) { ... }
But if we try to subscribe a method with the above signature to the event implementing EventHandler delegate:
public event EventHandler MyEvent;
, then we get an error. Why is that?
thanx
Suppose the code raising the event specified a value which wasn’t a
DerivedEventArgs– what would you expect it to do? Basically you’d lose type safety.EDIT Note that you can do it the other way round – you can subscribe to an event with a more specific parameter type using a method with a less specific parameter type – because the event is still guaranteeing that it will call the handler with something compatible. Here’s an example: