开发者

Why does variance in .NET 4 only support reference types?

.NET 4 supports co- and contravariance. However, only reference types are supported, not value types.开发者_StackOverflow社区 Why is that?


Basically the CLR needs to know that it can treat a value of the "source" type as a value of the "target" type without performing any extra conversions - to put it simply, the bit pattern for the source value has to be valid as the target value. The representations have to be the same. Otherwise the CLR would need to have extra information to perform the right conversions at the right time.

Eric Lippert has blogged about representation and identity - see that post for more information (as ever :).


Jon is right; to just expand on that a bit. Suppose you have

Func<int> f1 = ()=>2;
Func<object> f2 = f1;
object o1 = f1();
object o2 = f2();

Suppose that were legal. The compiler generates a boxing instruction; the third line is effectively:

object o1 = BoxIntegerToObject(f1());

Clearly the result stored in o2 should be a boxed int. Where is the boxing instruction? It cannot be after the call to f2, because that thing is guaranteed to return an object already. It cannot be in the call to f2, because the call to f2 is actually a call to f1, and f1 guarantees that it returns an unboxed int.

So the boxing instruction cannot happen after the call returns, and it cannot happen before the call returns, and therefore it cannot happen at all, and therefore this must be illegal. There is no reference conversion from Func<int> to Func<object>.


Both forms of variance are defined in terms of inheritance: value types aren't inherited.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜