{"id":4034,"date":"2014-03-30T07:19:44","date_gmt":"2014-03-30T07:19:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-using-new-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:19:44","modified_gmt":"2014-03-30T07:19:44","slug":"c-using-new-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-using-new-collection-of-common-programming-errors\/","title":{"rendered":"C# Using new[]-Collection of common programming errors"},"content":{"rendered":"<p>Okay, there&#8217;s still a <em>little<\/em> bit of confusion here.<\/p>\n<p>The inference that&#8217;s going on has nothing to with the type of Object.SomeProperty, but everything to do with the types of the expressions in the array initializer. In other words, you could do:<\/p>\n<pre><code>object o = new[] { \"string1\", \"string2\" };\n<\/code><\/pre>\n<p>and o would still be a reference to a string array.<\/p>\n<p>Basically, the compiler looks at an expression like this:<\/p>\n<pre><code>new[] { A, B, C, D, ... }\n<\/code><\/pre>\n<p>(where A, B, C, D etc are expressions) and tries to work out the correct array type to use. It only considers the types of A, B, C and D (etc) as the array element type. Taking this set of candidate types, it tries to find one which all the others can be implicitly converted to. If there&#8217;s not <em>exactly one<\/em> such type then the compiler will complain.<\/p>\n<p>So for example:<\/p>\n<pre><code>new[] { new Form(), new MemoryStream() }\n<\/code><\/pre>\n<p>will not compile &#8211; neither <code>MemoryStream<\/code> nor <code>Form<\/code> is convertible to the other. However:<\/p>\n<pre><code>new[] { GetSomeIDisposable(), new MemoryStream() }\n<\/code><\/pre>\n<p>will be treated as an <code>IDisposable[]<\/code> because there&#8217;s an implicit conversion from <code>MemoryStream<\/code> to <code>IDisposable<\/code>. Likewise:<\/p>\n<pre><code>new[] { 0, 1, 3.5 } \/\/ double[]\nnew[] { 1, 3, 100L } \/\/ long[]\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Okay, there&#8217;s still a little bit of confusion here. The inference that&#8217;s going on has nothing to with the type of Object.SomeProperty, but everything to do with the types of the expressions in the array initializer. In other words, you could do: object o = new[] { &#8220;string1&#8221;, &#8220;string2&#8221; }; and o would still be [&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-4034","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4034","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=4034"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4034\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}