{"id":1514,"date":"2016-09-15T22:34:35","date_gmt":"2016-09-16T02:34:35","guid":{"rendered":"http:\/\/codinggorilla.domemtech.com\/?p=1514"},"modified":"2016-09-16T09:57:51","modified_gmt":"2016-09-16T13:57:51","slug":"c-v7-0-pattern-matching","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2016\/09\/15\/c-v7-0-pattern-matching\/","title":{"rendered":"C# v7.0 pattern matching"},"content":{"rendered":"<p>Visual Studio &#8220;15&#8221; Preview 4 was recently released, so I decided to take it for a spin. In the upcoming\u00c2\u00a0C# 7.0, of the <a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/2136\" target=\"_blank\">features being implemented<\/a>, <a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md\" target=\"_blank\">pattern matching<\/a> is probably the most interesting. Consider\u00c2\u00a0how often we&#8217;ve designed code that uses a switch statement with complex cases, but then when we go to implement the design, a nested if-then-else statement must be coded instead\u00c2\u00a0because <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/06tc147t.aspx\" target=\"_blank\">switch labels must be constant expressions<\/a>. With pattern matching, this will finally\u00c2\u00a0change.<\/p>\n<p><em>In theory, when the feature is fully implemented<\/em>, we could write complex switch statements such as:<\/p>\n\n<p><em>Unfortunately, while this example compiles, it does not run properly yet, in Visual Studio &#8220;15&#8221; Preview 4.<\/em><\/p>\n<p>Years ago, I used to write C code that would contain declaration expressions. This would simultaneously assign a value and test to see if the value was non-zero. You can do this in C++, e.g.,<\/p>\n\n<p>In C#, we can use patterns to declare local variables within an expression, extending the declaration expression existing in C++:<\/p>\n\n<p>If the switch is rewritten as a nested <em>if-then-else<\/em>, the code works fine. Note, the scope of pattern matching variables is within the <em>then<\/em> clause, not the <em>else<\/em> clause of the <em>if<\/em> statement. I&#8217;m not sure why this would be <a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md\" target=\"_blank\">the design<\/a>, as in C\/C++, the scope of declaration expression variables extend into the <em>else<\/em> clause. But perhaps it is to allow reuse of the same variable in a nested <em>if-then-else<\/em>.<\/p>\n\n<h4>Further Information<\/h4>\n<p><a href=\"https:\/\/www.infoq.com\/news\/2016\/04\/CSharp-7\" target=\"_blank\">https:\/\/www.infoq.com\/news\/2016\/04\/CSharp-7<\/a><\/p>\n<p>Essential .NET &#8211; Designing C# 7,\u00c2\u00a0<a href=\"https:\/\/msdn.microsoft.com\/en-us\/magazine\/mt149362?author=mark+michaelis\">Mark Michaelis<\/a>,\u00c2\u00a0December 2015\u00c2\u00a0<a href=\"https:\/\/msdn.microsoft.com\/en-us\/magazine\/mt595758.aspx\" target=\"_blank\">https:\/\/msdn.microsoft.com\/en-us\/magazine\/mt595758.aspx<\/a><\/p>\n<p>Pattern Matching for C#\u00c2\u00a0<a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md\" target=\"_blank\">https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/2136\" target=\"_blank\">https:\/\/github.com\/dotnet\/roslyn\/issues\/2136<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/206\" target=\"_blank\">https:\/\/github.com\/dotnet\/roslyn\/issues\/206<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md\" target=\"_blank\">https:\/\/github.com\/dotnet\/roslyn\/blob\/future\/docs\/features\/patterns.md<\/a><\/p>\n<p class=\"general\">Advanced Pattern Matching Features Removed From C# 7\u00c2\u00a0<a href=\"https:\/\/www.infoq.com\/news\/2016\/05\/csharp7-pattern-matching-removed\" target=\"_blank\">https:\/\/www.infoq.com\/news\/2016\/05\/csharp7-pattern-matching-removed<\/a><\/p>\n<p class=\"general\">C# 7 Features Previewed\u00c2\u00a0<a href=\"https:\/\/www.infoq.com\/news\/2016\/04\/CSharp-7\" target=\"_blank\">https:\/\/www.infoq.com\/news\/2016\/04\/CSharp-7<\/a><\/p>\n<p>Unification: pattern matching, but twice as nice!\u00c2\u00a0By: <span class=\"author\"><a title=\"Posts by Frank Shearar\" href=\"http:\/\/www.lshift.net\/author\/frank\/\" rel=\"author\">Frank Shearar<\/a><\/span> on <span class=\"date\">May 31, 2011 \u00c2\u00a0<\/span><a href=\"http:\/\/www.lshift.net\/blog\/2011\/05\/31\/unification-pattern-matching-but-twice-as-nice\/\" target=\"_blank\">http:\/\/www.lshift.net\/blog\/2011\/05\/31\/unification-pattern-matching-but-twice-as-nice\/<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/master\/docs\/Language%20Feature%20Status.md\" target=\"_blank\">https:\/\/github.com\/dotnet\/roslyn\/blob\/master\/docs\/Language%20Feature%20Status.md<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio &#8220;15&#8221; Preview 4 was recently released, so I decided to take it for a spin. In the upcoming\u00c2\u00a0C# 7.0, of the features being implemented, pattern matching is probably the most interesting. Consider\u00c2\u00a0how often we&#8217;ve designed code that uses a switch statement with complex cases, but then when we go to implement the design, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/165.227.223.229\/index.php\/2016\/09\/15\/c-v7-0-pattern-matching\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C# v7.0 pattern matching&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1514"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=1514"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/1514\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}