{"id":8620,"date":"2020-11-14T14:16:59","date_gmt":"2020-11-14T14:16:59","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8620"},"modified":"2020-11-14T17:51:14","modified_gmt":"2020-11-14T17:51:14","slug":"c-9-0-record-type","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-9-0-record-type\/","title":{"rendered":"C# 9.0 record type"},"content":{"rendered":"<p>As of yesterday I got the updated version of Visual Studio 2019 (16.8.1) which includes .NET 5 and C# 9.0 &#8211; I don&#8217;t intend to post about every feature of C# 9.0 &#8211; for those interested simply go and take a look at <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/whats-new\/csharp-9\" rel=\"noopener noreferrer\" target=\"_blank\">What&#8217;s new in C# 9.0<\/a>.<\/p>\n<p>One feature that&#8217;s quite interesting is C# Records. <\/p>\n<p>Record types &#8220;are a reference type that provides synthesized methods to provide value semantics for equality&#8221;. So basically we can create multiple instances of a record and compare them for equality. Here&#8217;s an example of a record type<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic record Person\r\n{\r\n   public Person(string firstName, string lastName)\r\n   {\r\n      FirstName = firstName;\r\n      LastName = lastName;\r\n   }\r\n\r\n   public string LastName { get; }\r\n   public string FirstName { get; }\r\n}\r\n<\/pre>\n<p>It&#8217;s that easy, we simply declare our type as a <em>record<\/em> instead of a <em>class<\/em>. <\/p>\n<p>Now, if we create a couple of instances of a Person that look like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson person1 = new(&quot;Scooby&quot;, &quot;Doo&quot;);\r\nPerson person2 = new(&quot;Scooby&quot;, &quot;Doo&quot;);\r\n<\/pre>\n<p>and we compare them using ==\/Equals, they will result in the two instances being the same (unlike with classes with would compare equality by reference). <\/p>\n<p>Another feature of records, over classes is. If you use ToString() on an instance of a class you&#8217;ll see something like <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTestApp.Person\r\n<\/pre>\n<p>For a record type you&#8217;ll instead get <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nPerson { LastName = Doo, FirstName = Scooby }\r\n<\/pre>\n<p>this is facilitated by the compiler adding a PrintMembers protected method which generates this output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As of yesterday I got the updated version of Visual Studio 2019 (16.8.1) which includes .NET 5 and C# 9.0 &#8211; I don&#8217;t intend to post about every feature of C# 9.0 &#8211; for those interested simply go and take a look at What&#8217;s new in C# 9.0. One feature that&#8217;s quite interesting is C# [&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-8620","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\/8620","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=8620"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8620\/revisions"}],"predecessor-version":[{"id":8625,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8620\/revisions\/8625"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}