{"id":100335,"date":"2018-11-27T07:00:00","date_gmt":"2018-11-27T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100335"},"modified":"2019-03-13T00:14:41","modified_gmt":"2019-03-13T07:14:41","slug":"20181127-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20181127-00\/?p=100335","title":{"rendered":"Nifty trick: Combining constructor with collection initializer"},"content":{"rendered":"<p>C# provides a number of ways of <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/object-and-collection-initializers\">initializing collections<\/a>. <\/p>\n<p>If a collection has a single-parameter <code>Add<\/code> method, you can add items into the collection as part of initialization: <\/p>\n<pre>\nvar list = new List&lt;int&gt; { 1, 2, 3 };\n\n\/\/ Equivalent to\nvar list = new List&lt;int&gt;();\nlist.Add(1);\nlist.Add(2);\nlist.Add(3);\n<\/pre>\n<p>Note that if you do not provide an argument list after the type, <a HREF=\"https:\/\/en.wikipedia.org\/wiki\/Miranda_warning\">one will be provided for you<\/a>, namely <code>()<\/code>. <\/p>\n<p>If a collection has a multi-parameter <code>Add<\/code> method, you can add items into the collection as part of the initialization, but you need to put braces around each parameter list: <\/p>\n<p><pre>\nvar dict = new Dictionary&lt;int, int&gt; {\n    { 0, 1 },\n    { 1, 2 },\n};\n\n\/\/ Equivalent to\nvar dict = new Dictionary&lt;int, int&gt;();\ndict.Add(0, 1);\ndict.Add(1, 2);\n<\/pre>\n<p>If a collection has an index setter, you can add items into the collection with indexer syntax: <\/p>\n<pre>\nvar dict = new Dictionary&lt;int, int&gt; {\n    [0] = 1,\n    [1] = 2,\n};\n\n\/\/ Equivalent to\nvar dict = new Dictionary&lt;int, int&gt;();\ndict[0] = 1;\ndict[1] = 2;\n<\/pre>\n<p>You cannot combine these different initializer notations, however. <\/p>\n<pre>\n\/\/ Code in italics doesn't compile\n<i>var dict = new Dictionary&lt;int, int&gt; {\n    {0, 1 },\n    [1] = 2,\n};<\/i>\n<\/pre>\n<p>However, one thing that is sometimes interesting to do is combine the constructor with the collection initializer. This lets you clone a collection and then modify it. <\/p>\n<pre>\n\/\/ Resulting list is { 1, 2, 3, 4, 5 }\nvar list2 = new List&lt;int&gt;(list) { 4, 5 };\n\n\/\/ Resulting list is { 4, 2, 3 }\nvar list3 = new List&lt;int&gt;(list) { [0] = 4 };\n\n\/\/ Resulting dictionary is dict2[0] = 1, dict2[1] = 2,\n\/\/ and dict[2] = 3\nvar dict2 = new Dictionary&lt;int, int&gt;(dict) { [2] = 3 };\n\n\/\/ Resulting dictionary is dict2[0] = 4, dict2[1] = 2\nvar dict2 = new Dictionary&lt;int, int&gt;(dict) { [0] = 4 };\n<\/pre>\n<p>You can pass anything that is a valid constructor parameter. For example, <code>List&lt;T&gt;<\/code> permits construction from any enumerable, so you can do this: <\/p>\n<pre>\nstring[] ab = new string[] { \"a\", \"b\" };\nList&lt;string&gt; abcd = new List&lt;string&gt;(ab) { \"c\", \"d\" };\n\/\/ abcd has four elements: \"a\" \"b\" \"c\" and \"d\"\n<\/pre>\n<p>This combination notation is useful if you want to clone an existing collection and then make some tweaks to it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two great tastes that work great together.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Two great tastes that work great together.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=100335"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100335\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=100335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}