{"id":8627,"date":"2020-11-14T22:10:56","date_gmt":"2020-11-14T22:10:56","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8627"},"modified":"2020-11-14T22:10:56","modified_gmt":"2020-11-14T22:10:56","slug":"with-and-record-types-in-c-9-0","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/with-and-record-types-in-c-9-0\/","title":{"rendered":"with and record types in C# 9.0"},"content":{"rendered":"<p>In the previous post we looked at the new C# record type. Whilst we can make record types mutable, they&#8217;re particularly well suited to immutability. When used as immutable types we need a way to make new immutable types based upon previous ones.<\/p>\n<p>Let&#8217;s use our Person record type from the previous post but sprinkled with C# 9 goodness in the form of <em>init<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic record Person\r\n{\r\n   public string FirstName { get; init; }\r\n   public string LastName { get; init; }\r\n}\r\n<\/pre>\n<p>The <em>init<\/em> syntax gives us the ability to create instances of a Person, assigning values during the construction phase of the record type using standard initialiser syntax. <\/p>\n<p>For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar p = new Person \r\n{ \r\n   FirstName = &quot;Scooby&quot;, \r\n   LastName = &quot;Doo&quot; \r\n};\r\n<\/pre>\n<p>However, this post is mean&#8217;t to be about the <em>with<\/em> keyword with allows us to take a record type and create a new instance baed upon and existing record with changes. i.e. we want to take the Person <em>p<\/em> and create a new record with the FirstName = &#8220;Scrappy&#8221;, like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar scrappy = p with { FirstName = &quot;Scrappy&quot; };\r\n<\/pre>\n<p>the result of outputting this to the console would be<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson { FirstName = Scrappy, LastName = Doo }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post we looked at the new C# record type. Whilst we can make record types mutable, they&#8217;re particularly well suited to immutability. When used as immutable types we need a way to make new immutable types based upon previous ones. Let&#8217;s use our Person record type from the previous post but sprinkled [&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-8627","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\/8627","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=8627"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8627\/revisions"}],"predecessor-version":[{"id":8630,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8627\/revisions\/8630"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}