{"id":3979,"date":"2014-03-30T06:56:41","date_gmt":"2014-03-30T06:56:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-can-i-cast-the-invariance-of-ilistt-away-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:56:41","modified_gmt":"2014-03-30T06:56:41","slug":"why-can-i-cast-the-invariance-of-ilistt-away-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-can-i-cast-the-invariance-of-ilistt-away-collection-of-common-programming-errors\/","title":{"rendered":"Why can I cast the invariance of IList&lt;T&gt; away?-Collection of common programming errors"},"content":{"rendered":"<p>Currently I&#8217;m preparing a presentation of the new generic variance features in C# for my colleagues. To cut the story short I wrote following lines:<\/p>\n<pre><code>IList formsList = new List { new Form(), new Form() };\nIList controlsList = formsList;\n<\/code><\/pre>\n<p>Yes, this is of course not possible, as IList(Of T) is invariant (at least my thought). The compiler tells me that: <code>Cannot implicitly convert type 'System.Collections.Generic.IList' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)<\/code><\/p>\n<p>Hm, does this mean I can force an explicit conversion? I just tried it:<\/p>\n<pre><code>IList formsList = new List { new Form(), new Form() };\nIList controlsList = (IList)formsList;\n<\/code><\/pre>\n<p>And. it compiles! Does it mean I can cast the invariance away? &#8211; At least the compiler is ok with that, but I just turned the former compile time error to a run time error:<\/p>\n<blockquote>\n<p><code>Unable to cast object of type 'System.Collections.Generic.List`1[System.Windows.Forms.Form]' to type 'System.Collections.Generic.IList`1[System.Windows.Forms.Control]'.<\/code><\/p>\n<\/blockquote>\n<p>My question(s): Why can I cast the invariance of <code>IList<\/code> (or any other invariant interface as to my experiments) away? Do I really cast the invariance away, or what kind of conversion happens here (as <code>IList(Of Form)<\/code> and <code>IList(Of Control)<\/code> are completely unrelated)? Is this a dark corner of C# I didn&#8217;t know?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently I&#8217;m preparing a presentation of the new generic variance features in C# for my colleagues. To cut the story short I wrote following lines: IList formsList = new List { new Form(), new Form() }; IList controlsList = formsList; Yes, this is of course not possible, as IList(Of T) is invariant (at least my [&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-3979","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3979","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=3979"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3979\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}