{"id":11,"date":"2015-04-18T19:11:00","date_gmt":"2015-04-18T19:11:00","guid":{"rendered":""},"modified":"2016-01-30T22:43:05","modified_gmt":"2016-01-30T22:43:05","slug":"nested-properties-initialization-syntactic-sugar","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2015\/04\/18\/nested-properties-initialization-syntactic-sugar\/","title":{"rendered":"Nested properties initialization &#8211; syntactic sugar"},"content":{"rendered":"<p>I think most of developers are familiar with the object syntax initializer, which basically allows you to set up values of properties just like that<\/p>\n<pre lang=\"csharp\">\r\nvar webClient = new CustomWebClient\r\n            {\r\n                Settings = new CustomWebClientSettings\r\n                {\r\n                    Encoding = \"UTF-8\",\r\n                    Method = \"POST\"\r\n                }\r\n            };\r\n<\/pre>\n<p>In this example the <i>Settings<\/i> property is assigned with new instance of <i>CustomWebClientSettings<\/i> class. However for time to time we only want to change particular property of given nested property and leave rest of the object intact. Of course we can write it usual way<\/p>\n<pre lang=\"csharp\">\r\n\/\/ we assume that CustomWebClient initialized Settings property in constructor\r\nvar webClient = new CustomWebClient();\r\nwebClient.Settings.Encoding = \"UTF-8\";\r\nwebClient.Settings.Method = \"POST\";\r\n<\/pre>\n<p>but there is a much cooler syntax for that. The code above can be rewritten to<\/p>\n<pre lang=\"csharp\">\r\nvar webClient = new CustomWebClient\r\n            {\r\n                 Settings = {Encoding = \"UTF-8\", Method = \"POST\"}\r\n            };\r\n<\/pre>\n<p>Note that there is no <i>new<\/i> keyword in the assignment of <i>Settings<\/i> property, which means that the <i>Settings<\/i> object is updated with the values of properties in braces. Of course this syntax can be safely used only if the nested property(in this case <i>Settings<\/i>) is initialized up front in constructor, otherwise you will get <i>NullReferenceException<\/i>. Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/NestedPropertiesInitialization\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think most of developers are familiar with the object syntax initializer, which basically allows you to set up values of properties just like that var webClient = new CustomWebClient { Settings = new CustomWebClientSettings { Encoding = &#8220;UTF-8&#8221;, Method = &#8220;POST&#8221; } }; In this example the Settings property is assigned with new instance [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[160,159],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-net","category-c","tag-net","tag-c"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":3,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":500,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions\/500"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}