Updated April 13, 2023
Introduction to C# Pattern Matching
A feature in C# used to match any data or any object is called pattern matching and this pattern matching is performed using the expression is and switch statement where is expression checks if the data or object has compatibility with the type that is specified or not and switch statement is used to match the different patterns in C#. By using the expression is and switch statement for pattern matching in C#, the way the applications are written can be changed to a more readable manner, easier to maintain, and in a manner easy to understand.
Methods of C# Pattern Matching
There are two methods of pattern matching in C#. They are:
1. is expression
is expression is used to check the compatibility of the data or object with the type that is specified.
Example #1
C# program to Demonstrate is an Expression in a Program.
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called subject is defined in which a subject is assigned to a string variable
class Subject
{
public string SubName{ get; set; } = "C Sharp";
}
//a class called check is defined
class check
{
//main method is called
public static void Main(string[] args)
{
//an instance of the subject class is created
Subject sub = new Subject();
//is expression is used to check if the instance of the subject class is compatible with the type of the value assigned to the string variable in subject class
if(sub is Subject)
{
Console.WriteLine(sub.SubName);
}
}
}
}
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called Subject is defined in which a subject name is assigned to a string variable. Then a class called check is defined in which the main method is called. Then an instance of the subject class is created. Then is expression is used to check if the newly created instance of the subject class is compatible with the type of the value assigned to the string variable in class subject. If it is compatible, the subject name is displayed as the output. The output of the program is shown in the snapshot above.
Example #2
C# program to demonstrate is Expression in a Program.
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called Writer is defined in which a name of the writer is assigned to a string variable
class Writer
{
public string WriterName{ get; set; } = "ShobhaShivakumar";
}
//a class called check is defined
class check
{
//main method is called
public static void Main(string[] args)
{
//an instance of the Writer class is created
Writer write = new Writer();
//is expression is used to check if the instance of the Writer class is compatible with the type of the value assigned to the string variable in Writer class
if(write is Writer)
{
Console.WriteLine(write.WriterName);
}
}
}
}
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called Writer is defined in which the name of the writer is assigned to a string variable. Then a class called check is defined in which the main method is called. Then an instance of the Writer class is created. Then is expression is used to check if the newly created instance of the Writer class is compatible with the type of the value assigned to the string variable in class Writer. If it is compatible, the name of the writer is displayed as the output. The output of the program is shown in the snapshot above.
2. Switch Statement
The switch statement is used to match different patterns in C#.
Example #1
C# Program to Demonstrate a Switch Statement in a Program.
Code:
using System;
//a class called check is defined
class check
{
//main method is called
public static void Main()
{
//a name is assigned to a string variable
string val = "Shobha_Shivakumar";
//switch statement is used to switch between the values that is assigned to the string variable and anything else
switch (val)
{
case "Shobha_Shivakumar":
Console.WriteLine("The assigned value is Shobha_Shivakumar");
break;
case "not_assigned":
Console.WriteLine("The assigned value is not_assigned");
break;
}
}
}
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called in which a name is assigned to a string variable. Then the switch statement is used to switch between the values that is assigned to the string variable and anything else. If the name assigned to the string variable is the switch case, then the corresponding output is displayed. Likewise, if it is anything else, the corresponding output is displayed. The output of the program is as shown in the snapshot above.
Example #2
C# Program to Demonstrate Switch Statement in a Program:
Code:
using System;
//a class check is defined
class Check
{
//main method is called
static void Main()
{
while(true)
{
Console.WriteLine("Type any alphabet between A and Z");
try
{
//a string is expected as the input by the user
string r = Console.ReadLine();
//switch statement is used to parse the input given by the user and display the output accordingly
switch(r)
{
case "A":
{
Console.WriteLine("This is Alphabet A");
break;
}
case "B":
{
Console.WriteLine("This is alphabet B");
break;
}
default:
{
Console.WriteLine("This is something other than Alphabets A and B");
break;
}
}
}
catch
{
}
}
}
}
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called in which a string between the alphabets A to Z is expected as the input from the user. Then the switch statement is used to parse the input provided by the user and to display the output accordingly. If the input is any alphabet other than A and B, the same is displayed in the output. The output of the above program is shown in the snapshot above.
Conclusion
In this tutorial, we understand the concept of Pattern Matching in C# through definitions and methods of pattern matching through examples and their outputs.
Recommended Article
This is a guide to C# Pattern Matching. Here we discuss the Introduction to C# Pattern Matching and its methods along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –