Generic Type Variance in C# and the CLR

Andrew Stopford has an interesting couple of posts on the CLR’s support for generic type variance[1] in version 2.0, and the lack of which in C# 2.0. I join Andrew in wishing this would be implemented in C# 3.0 (or at least in an interim ‘2.1’ release with .NET 3.0).
Be sure to read the comments on those posts too for some more in-depth information about this.

[1] Generic type variance means that List<string> is a List<object>, just like string[] is a object[].


One thought on “Generic Type Variance in C# and the CLR

  1. I'm glad this topic is being raised with some regularity. But I don't think the CLR's current implementation of variance is actually enough to cut it. I've spent some time learning the way Java handles variance in generics and while Java got almost everything else about generics *wrong* (erasure? ugh!), they got this right. In reality a List<string> *isn't* a List<object>, because if it were, you could put an int into it. What you really want is a way, within the type system, to refer to "a list of *something* that inherits from object". Java's syntax is ugly – List<? extends Object> – but it expresses exactly the right concept. Java calls these "wildcard types". And the CLR needs them. Badly.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s