C# 4.0: Covariance and Contravariance on generics…sort of…
One of the things that we’ll have in C# 4.0 is covariance/contravariace working on generics…I mean, sort of. Why do I say sort of? Well, because you’ll have to use the identifiers in and out for explicitly stating if you’ll be having covariance or contravariance and you can’t use them at the some time. Charlie Calvert has a good post on this subject, so you should read it before we go on.
Don’t get me wrong: having this basic support is better than having none, but I expected a little more from the C# team. I’m sure there’s a valid reason for making us use the in an out terms, but personally, I’d really prefer not to use any and let the damn thing be automatically infered. But to me that is not the worst part… what I really don’t like is that (and please tell me I’m wrong!) you cannot annotate a generic type with in and out simultaneously. In practice, this means that you should be able to use both features with simple interfaces (ex.: IEnumerable) but you won’t be able to use them withthe current collection interfaces because you’ll have cases where you want to use covariance and contravariance at the same time on a specific type…
btw, Eric Lippert has a great series on this topic.
Thoughts?
Update: Ok, didn’t I said that Eric had a great series? Yes he does and he convinced me that we need to think about it explicitly. Not convinced about not being able to say that a parameter can be in and out at the same time…