
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is Pattern Matching in C# 7.0?
C# 7.0 introduces pattern matching in two cases, the is expression and the switch statement.
Patterns test that a value has a certain shape, and can extract information from the value when it has the matching shape.
Pattern matching provides more concise syntax for algorithms
You can perform pattern matching on any data type, even your own, whereas with if/else, you always need primitives to match.
Pattern matching can extract values from your expression.
Before Pattern Matching −
Example
public class PI{ public const float Pi = 3.142f; } public class Rectangle : PI{ public double Width { get; set; } public double height { get; set; } } public class Circle : PI{ public double Radius { get; set; } } class Program{ public static void PrintArea(PI pi){ if (pi is Rectangle){ Rectangle rectangle = pi as Rectangle; System.Console.WriteLine("Area of Rect {0}", rectangle.Width * rectangle.height); } else if (pi is Circle){ Circle c = pi as Circle; System.Console.WriteLine("Area of Circle {0}", Circle.Pi * c.Radius * c.Radius); } } public static void Main(){ Rectangle r1 = new Rectangle { Width = 12.2, height = 33 }; Rectangle r2 = new Rectangle { Width = 12.2, height = 44 }; Circle c1 = new Circle { Radius = 12 }; PrintArea(r1); PrintArea(r2); PrintArea(c1); Console.ReadLine(); } }
Output
Area of Rect 402.59999999999997 Area of Rect 536.8 Area of Circle 452.44799423217773
After Pattern Matching −
Example
public class PI{ public const float Pi = 3.142f; } public class Rectangle : PI{ public double Width { get; set; } public double height { get; set; } } public class Circle : PI{ public double Radius { get; set; } } class Program{ public static void PrintArea(PI pi){ if (pi is Rectangle rectangle){ System.Console.WriteLine("Area of Rect {0}", rectangle.Width * rectangle.height); } else if (pi is Circle c){ System.Console.WriteLine("Area of Circle {0}", Circle.Pi * c.Radius * c.Radius); } } public static void Main(){ Rectangle r1 = new Rectangle { Width = 12.2, height = 33 }; Rectangle r2 = new Rectangle { Width = 12.2, height = 44 }; Circle c1 = new Circle { Radius = 12 }; PrintArea(r1); PrintArea(r2); PrintArea(c1); Console.ReadLine(); } }
Output
Area of Rect 402.59999999999997 Area of Rect 536.8 Area of Circle 452.44799423217773
- Related Articles
- Wildcard Pattern Matching
- What is MySQL REGEXP operator and how it handles pattern matching?
- Unix filename pattern matching in Python
- Pattern matching in C# with Regex
- Pattern matching in Python with Regex
- Unix filename pattern matching in Python (fnmatch)
- Lua pattern matching vs regular expression
- Which package is used for pattern matching with regular expressions in java?
- Program to check regular expression pattern is matching with string or not in Python
- MySQL pattern matching 3 or more “a's” in name?
- Print all words matching a pattern in CamelCase Notation Dictionary in C++
- What is matching concept in finance & accounting?
- What is sequential pattern mining?
- Golang Program to get the List of filenames matching the specified wild card pattern
- C++ Program for triangular pattern (mirror image around 0)

Advertisements