Introduction
C# pattern matching is a feature that allows us to perform matching on data or any object. We can perform pattern matching using the Is expression and Switch statement. Is expression is used to check, whether an object is compatible with the given type or not? A switch statement has been enhanced with the const pattern, the type pattern, and the var pattern.
“is” Operator with Pattern Matching
The “is” operator is available since the first version of C#. This operator can be used to check if an object is compatible with a specific type. With the C# 7.0 extensions, the “is” operator can be used to check for patterns.
Const Pattern
One available pattern in C# 7.0 is the const pattern. The below example will explain the const pattern.
-
- if (o is null) Console.WriteLine($ "Object is null");
- if (o.ValConstant is 32) Console.WriteLine($” o.ValConstant is 32 ");
Type Pattern
With this pattern, you can verify if the object is compatible with the specific type.
-
- if (o.ValConstant is int i) Console.WriteLine($ "its a type pattern with an int and its value = {i}");
- if (o is Associate A) Console.WriteLine($ "its a type pattern with Associate and FirstName ={A.FirstName}");
Var Pattern
This type is using the var keyword. Here the object is always type.
-
- if (o is
- var aa) Console.WriteLine($ "its a var type type : {aa?.GetType()?.Name}");
- namespace PatternMatching {
- class Program {
- static void Main(string[] args) {
- var records = new List < Associate > {
- new Associate("Prasad", 32, null),
- new Associate("Praveen", 58, null)
- };
- foreach(var obj in records) {
- IsPatternMatching(obj);
- }
- }
- private static void IsPatternMatching(Associate o) {
-
- if (o is null) Console.WriteLine($ "Object is null");
- if (o.ValConstant is 32) Console.WriteLine($ "{o.ValConstant} is constant");
-
- if (o.ValConstant is int i) Console.WriteLine($ "its a type pattern with an int and its value = {i}");
- if (o is Associate A) Console.WriteLine($ "its a type pattern with Associate and FirstName ={A.FirstName}");
-
- if (o is
- var aa) Console.WriteLine($ "its a var type type : {aa?.GetType()?.Name}");
- }
- }
- public class Associate {
- public string FirstName {
- get;
- set;
- }
- public int ValConstant {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public Associate(string firstName, int value, string lastName) {
- FirstName = firstName;
- ValConstant = value;
- LastName = lastName;
- }
- }
- }
“switch” Operator with Pattern Matching
The switch statement has been enhanced with const, type, and var pattern.
Const Pattern
Using case statement, const pattern can be implemented.
-
-
-
- private static void ConstantPatternMatching() {
- object obj = 34;
- switch (obj) {
- case 34:
- Console.WriteLine($ "obj is constant");
- break;
- }
- }
Type Pattern
It checks if an object is of the specified type
-
-
-
- private static void TypePatternMatching() {
- Object obj = Console.Error;
- switch (obj) {
- case TextWriter textwriter:
- textwriter.WriteLine($ "Object is TextWriter");
- break;
- }
- }
Var Pattern and Conditional clause
It always matches and puts the value into a new variable
-
-
-
- private static void VarPatternMatching() {
- int obj = 100;
- switch (obj) {
- case var obj1 when obj1 > 50: Console.WriteLine($ "obj1 is a variant of value {obj1}");
- break;
- }
- }
Full source code given below:
- namespace PatternMatching {
- class Program {
- static void Main(string[] args) {
- ConstantPatternMatching();
- TypePatternMatching();
- VarPatternMatching();
- }
-
-
-
- private static void ConstantPatternMatching() {
- object obj = 34;
- switch (obj) {
- case 34:
- Console.WriteLine($ "obj is constant");
- break;
- }
- }
-
-
-
- private static void TypePatternMatching() {
- Object obj = Console.Error;
- switch (obj) {
- case TextWriter textwriter:
- textwriter.WriteLine($ "Object is TextWriter");
- break;
- }
- }
-
-
-
- private static void VarPatternMatching() {
- int obj = 100;
- switch (obj) {
- case var obj1 when obj1 > 50: Console.WriteLine($ "obj1 is a variant of value {obj1}");
- break;
- }
- }
- }
- }
Summary
In C# 7.0, pattern matching gives us yet another feature that can simplify and reduce your code. Furthermore, “is” and “switch/case” have been enhanced to support const, type, and var patterns.