I got an e-mail today from someone asking a question about being able to wireup all the events of a control to a single method, for example – a ListBox. In the current version of the framework this isn’t possible because the delegate types that constrain the event vary meaning that one single method signature wouldn’t be acceptable to all. For example – the following code wouldn’t work:

this.m_ListBox.MeasureItem += new EventHandler(this.MyEventHandler);
private void MyEventHandler(object sender, EventArgs e) {};

In an OO platform like this its kind of counter-intuitive that you would have this restriction - after all EventArgs is the base class for MeasureItemEventArgs – why can’t the runtime gracefully cast MeasureItemEventArgs to EventArgs for us – isn’t that what the events pattern is all about?

Well – it turns out that the CLR team agrees with us and so Program Manager and fellow Tim Tam eater Joel Pobar became responsible for that feature (along with many others) inside the next version of the CLR that is going to be shipping with Whidbey. The feature is called Covariance and Contravariance.

I found this post by Wesner Moise which sums up the feature really nicely (sorry Joel, I’m sure you posted about this but I couldn’t find it – IINIGIDE).