Question

I'm reading up on Variance in .NET at: http://msdn.microsoft.com/en-us/library/dd799517.aspx

I've come across this line: Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

Why is this? And can we not "box" the value type into a reference type or does this cause further issues?

I'm only JUST beginning to grasp the concept of variance so my understanding of it is very basic/incomplete.

Was it helpful?

Solution

If you consider that a value type

cannot inherit from another struct

http://msdn.microsoft.com/library/ah19swz4%28v=vs.90%29.aspx

it makes no sense talking about variance for them. If you box it into a reference type what type do you think to use, other than object? You should define an implicit or explicit cast to a reference type but for what is worth?

Variance is about polymorphism and inheritance. Consider that when you assign an int to a variable of type long you have implicit cast: there's no inheritance relationship between the two types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top