Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is because as of yet, C# does not support <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#C.23" rel="nofollow noreferrer">Covariance</a>.</p> <blockquote> <p>More formally, in C# v2.0 if T is a subtype of U, then T[] is a subtype of U[], but G is not a subtype of G (where G is any generic type). In type-theory terminology, we describe this behavior by saying that C# array types are “covariant” and generic types are “invariant”.</p> </blockquote> <p><em>Reference: <a href="http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx</a></em></p> <p>If you have the following method :</p> <pre><code>public static void AllDoSomething(List&lt;StuffBase&gt; items) { items.ForEach(i =&gt; i.DoSomething()); } var items = new List&lt;Stuff2&gt;(); x.AllDoSomething(items); //Does not compile </code></pre> <p>Where as if you use the generic type constraint, it will.</p> <p>For more information about Covariance and Contravariance], check out <a href="http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx" rel="nofollow noreferrer">Eric Lippert's series of posts</a>.</p> <hr> <p>Other posts worth reading : </p> <ul> <li><a href="http://www.pabich.eu/blog/archive/2008/02/12/c-generics---parameter-variance-its-constraints-and-how-it.aspx" rel="nofollow noreferrer">http://www.pabich.eu/blog/archive/2008/02/12/c-generics---parameter-variance-its-constraints-and-how-it.aspx</a></li> <li><a href="http://blogs.msdn.com/rmbyers/archive/2006/06/01/613690.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/rmbyers/archive/2006/06/01/613690.aspx</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/ms228359(VS.80).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms228359(VS.80).aspx</a></li> <li><a href="http://www.csharp411.com/convert-between-generic-ienumerablet/" rel="nofollow noreferrer">http://www.csharp411.com/convert-between-generic-ienumerablet/</a></li> <li><a href="http://research.microsoft.com/apps/pubs/default.aspx?id=64042" rel="nofollow noreferrer">http://research.microsoft.com/apps/pubs/default.aspx?id=64042</a></li> <li><a href="https://stackoverflow.com/questions/1169215/why-cant-listparent-listchild">Why can&#39;t List&lt;parent&gt; = List&lt;child&gt;?</a></li> </ul>
 

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