Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically, variance applies when the CLR can ensure that it doesn't need to make any <em>representational change</em> to the values. References all look the same - so you can use an <code>IEnumerable&lt;string&gt;</code> as an <code>IEnumerable&lt;object&gt;</code> without any change in representation; the native code itself doesn't need to know what you're doing with the values at all, so long as the infrastructure has guaranteed that it will definitely be valid.</p> <p>For value types, that doesn't work - to treat an <code>IEnumerable&lt;int&gt;</code> as an <code>IEnumerable&lt;object&gt;</code>, the code using the sequence would have to know whether to perform a boxing conversion or not.</p> <p>You might want to read Eric Lippert's <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx">blog post on representation and identity</a> for more on this topic in general.</p> <p>EDIT: Having reread Eric's blog post myself, it's at least as much about <em>identity</em> as representation, although the two are linked. In particular:</p> <blockquote> <p>This is why covariant and contravariant conversions of interface and delegate types require that all varying type arguments be of reference types. To ensure that a variant reference conversion is always identity-preserving, all of the conversions involving type arguments must also be identity-preserving. The easiest way to ensure that all the non-trivial conversions on type arguments are identity-preserving is to restrict them to be reference conversions.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload