Search code examples
c#clrc#-4.0covariancecontravariance

Why is C# 4.0's covariance/contravariance limited to parameterized interface and delegate types?


Is this a limitation of the CLR or are there compatibility concerns with existing code?

Is this related to the messed up variance of delegate combining in C# 4.0?

Edit: Would it be possible to have a language using co-/contravariance without that limitation running on the CLR?


Solution

  • Simple answer: it's a CLR limitation.

    (I haven't seen a good, concrete explanation for this anywhere... I don't remember seeing one in Eric's blog series about it, although I may well have missed it somewhere.)

    One thing I would say is that both delegates and interfaces already form "layers of indirection" over the real types; views on methods or classes, if you will. Changing from one view to another view is fairly reasonable. The actual class feels like a more concrete representation to me - and shifting from one concrete representation to another feels less reasonable. This is a very touchy-feely explanation rather than a genuine technical limitation though.