{"id":6902,"date":"2019-11-03T21:39:30","date_gmt":"2019-11-03T21:39:30","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6902"},"modified":"2019-11-03T21:39:30","modified_gmt":"2019-11-03T21:39:30","slug":"c-8-0-enhancements-with-pattern-matching","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-8-0-enhancements-with-pattern-matching\/","title":{"rendered":"C# 8.0 enhancements with pattern matching"},"content":{"rendered":"<p>C# 8.0 language features include pattern matching similar to that within F#. <\/p>\n<p>Tuples have become a first class citizen of C# 8.0, so we can write something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic (string, int) CreateTuple(string s)\r\n{\r\n   return (s, s.Length);\r\n}\r\n<\/pre>\n<p>This results in a ValueType being created, which can still be accessed using properties Item1 and Item2, however we can also use tuples within pattern matching, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar t = CreateTuple(&quot;Ten&quot;);\r\nswitch (t)\r\n{\r\n   case (string s, 1):\r\n      return $&quot;Single Character {s}&quot;;\r\n   case (string s, int l):\r\n      return $&quot;{s}:{l}&quot;);\r\n}      \r\n<\/pre>\n<p>In the above we&#8217;re matching firstly against a tuple where the first argument is a string and the second an int with value 1, the second case statements is basically going to catch everything else. Obviously the &#8220;catch all&#8221; must go after the more specific pattern match.<\/p>\n<p>This can be tidied further by using expressions. So the above becomes<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nreturn t switch\r\n{\r\n   (string s, 1) =&gt; $&quot;Single Character {s}&quot;,\r\n   (string s, int l) =&gt; $&quot;{s}:{l}&quot;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C# 8.0 language features include pattern matching similar to that within F#. Tuples have become a first class citizen of C# 8.0, so we can write something like this public (string, int) CreateTuple(string s) { return (s, s.Length); } This results in a ValueType being created, which can still be accessed using properties Item1 and [&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-6902","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\/6902","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=6902"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6902\/revisions"}],"predecessor-version":[{"id":7602,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6902\/revisions\/7602"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}