{"id":4240,"date":"2014-03-30T09:25:22","date_gmt":"2014-03-30T09:25:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/casting-a-listt-where-t-ibar-to-icollectionibar-fails-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:25:22","modified_gmt":"2014-03-30T09:25:22","slug":"casting-a-listt-where-t-ibar-to-icollectionibar-fails-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/casting-a-listt-where-t-ibar-to-icollectionibar-fails-collection-of-common-programming-errors\/","title":{"rendered":"Casting a List&lt;T&gt; (where T : IBar) to ICollection&lt;IBar&gt; fails-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>Why doesn&#8217;t this work?: <code>var foo = (ICollection )list;<\/code><\/p>\n<\/blockquote>\n<p>Let&#8217;s say <code>T = Foo<\/code> and there&#8217;s a second class <code>Foo2 : IBar<\/code>.<\/p>\n<p>Then you could continue like this:<\/p>\n<pre><code>var foolist = (ICollection )list;\nfoolist.Add(new Foo2());  \/\/ compiles, since Foo2 also implements IBar\n<\/code><\/pre>\n<p>Wham! You have a type violation at runtime, since you tried to add a <code>Foo2<\/code> to a <code>List<\/code>.<\/p>\n<p>To avoid this, <code>ICollection<\/code> is <em>not<\/em> a subtype of <code>ICollection<\/code>, even though <code>Foo<\/code> is a subtype of <code>IBar<\/code>. The theory behind this is co- and contravariance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why doesn&#8217;t this work?: var foo = (ICollection )list; Let&#8217;s say T = Foo and there&#8217;s a second class Foo2 : IBar. Then you could continue like this: var foolist = (ICollection )list; foolist.Add(new Foo2()); \/\/ compiles, since Foo2 also implements IBar Wham! You have a type violation at runtime, since you tried to add [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4240","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4240"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4240\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}