Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

Pattern Matching in C# 7

11/17/2017

Comments

 
C# 7 finally introduced a feature called "Pattern" matching. C# language introduced the notion of a pattern that can be used in is-expression and inside a case block of a switch statement. With C# 7 three distinct matching formats are now possible.
First format is the normal one which we always use and had been there right from the first release of C#.

New pattern matching enables matching based on a specific class or structure. Inline with the expression you can even include the new variable instance which can be accessed within the next code block

Now the third format is the most interesting one i.e. var patterns. It uses the var keyword, and simply copies the source test variable or expression into a new, named variable

Above code is not useful unless you include the "when" clause, which is also new to C# 7.

With new C# 7 switch statement has become more powerful and a goto option for flow control processing with expression.
​Hope you liked the post and do share it. Happy Coding!!!
Comments
comments powered by Disqus

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot