{"id":9483,"date":"2022-10-09T09:35:42","date_gmt":"2022-10-09T09:35:42","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9483"},"modified":"2022-10-09T09:35:42","modified_gmt":"2022-10-09T09:35:42","slug":"the-with-expression-in-c-9-0-and-later","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/the-with-expression-in-c-9-0-and-later\/","title":{"rendered":"The with expression in C# 9.0 and later"},"content":{"rendered":"<p>I&#8217;ve not had cause to use this so far. Which seems strange (in a way) as I used similar language features with the spread operator and changing values in TypeScript a lot. <\/p>\n<p>The <strong>with<\/strong> expression basically copies a record, struct or anonymous type and allows us to change some values, hence creating a new instance but overriding certain properties or fields.<\/p>\n<p><em>Note: In C# 9.0 the <strong>with<\/strong> expression only worked with record types, in C# 10 it can be used, additionally, with struct and anonymous types.<\/em><\/p>\n<p>Let&#8217;s look at a simple example. We have my old favourite demo type, a Person record which looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic record Person(string Name, int Age);\r\n<\/pre>\n<p>Now we&#8217;ll create a Person like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson scooby = new(&quot;Scooby Doo&quot;, 7);\r\n\r\nConsole.WriteLine(scooby);\r\n<\/pre>\n<p>This would ofcouse output something like <em>Person { Name = Scooby Doo, Age = 7 }<\/em>.<\/p>\n<p><em>Note: Obviously this is a rather simplistic record but it should give you the idea how things work. For more complex cases imagine there&#8217;s a first name, last name, lines of an address, national insurance\/social security number etc. just to make it a little more work to create copies of a Person record instance.<\/em><\/p>\n<p>Now let&#8217;s say we wanted to create a new record which has all the same properties\/fields bar some properties\/fields that will be changed. Ofcourse we could just create a new Person in the same way as above and change the properties. However, the <strong>with<\/strong> expression allows us to copy and change in a more elegant way (well more elegant if we had lots more properties\/fields on the record than this example).<\/p>\n<p>We can write<\/p>\n<pre class=\"brush: csharp; highlight: [2]; title: ; notranslate\" title=\"\">\r\nPerson scooby = new(&quot;Scooby Doo&quot;, 7);\r\nPerson scrappy = scooby with { Name = &quot;Scrappy Doo&quot; };\r\n\r\nConsole.WriteLine(scooby);\r\nConsole.WriteLine(scrappy);\r\n<\/pre>\n<p>The <em>scooby<\/em> instance remains unchanged as we&#8217;ve essentially created a new instance of the Person record, copying the Age and explicitly changing the Name.<\/p>\n<p>Now, what if we wanted to simply create a new instance of a Person with the same properties and fields as the <em>scooby<\/em> instance? We can just use the { } syntax like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson scoobyCopy = scooby with { };\r\n\r\nAssert.IsFalse(Object.ReferenceEquals(scooby, scoobyCopy));\r\n<\/pre>\n<p>As the Assert states, <em>scooby<\/em> and <em>scoobyCopy<\/em> instances are not the same instance, they are copies.<\/p>\n<p><em>Note: If using a struct (or if you&#8217;re explicitly supplying properties for a record) as the left-hand of the <strong>with<\/strong> expression and change values within the curly braces, you will need properties to have setters.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve not had cause to use this so far. Which seems strange (in a way) as I used similar language features with the spread operator and changing values in TypeScript a lot. The with expression basically copies a record, struct or anonymous type and allows us to change some values, hence creating a new instance [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-9483","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9483"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9483\/revisions"}],"predecessor-version":[{"id":9487,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9483\/revisions\/9487"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}