Pattern Matching can be used to extract information from a variable or to determine a variable’s type.
We will define a number of structs and use pattern matching to use the same function on them as an example. In this case, pattern matching will offer a shorter solution than abstract classes, where a complete class hierarchy would have had to be defined.
using System;class PatternMatching{// Defining structspublic struct Rectangle{public double Length { get; set;}public double Height { get; set;}public Rectangle(double l, double h){Length = l;Height = h;}}public struct Circle{public double Radius { get; set;}public Circle(double r){Radius = r;}}// Defining functions with parameter type objectstatic public double getArea(object Shape){// Pattern matching in If Statements// Variables also declared after casting in If Statementif (Shape is Circle c){return 3.142 * c.Radius * c.Radius;}else if(Shape is Rectangle r){return r.Length * r.Height;}return 0;}static public double getPerimeter(object Shape){// Pattern matching in If Statements// Variables also declared after casting in If Statementif (Shape is Circle c){return 2 * 3.142 * c.Radius;}else if(Shape is Rectangle r){return r.Length * 2 + r.Height * 2;}return 0;}static void Main(){// Declaring variablesCircle c = new Circle();c.Radius = 5;Rectangle r = new Rectangle();r.Height = 3;r.Length = 4;// The same function is called for both variablesSystem.Console.WriteLine(getArea(r));System.Console.WriteLine(getArea(c));System.Console.WriteLine(getPerimeter(r));System.Console.WriteLine(getPerimeter(c));}}
This program uses the is
operator in if
statements to check if the variable is of a certain type. The if
statement then casts the object into the appropriate type. With this, the correct areas and perimeters are returned for their respective shapes.
using System;class PatternMatching{// Defining structspublic struct Rectangle{public double Length { get; set;}public double Height { get; set;}public Rectangle(double l, double h){Length = l;Height = h;}}public struct Circle{public double Radius { get; set;}public Circle(double r){Radius = r;}}// Defining functions with parameter type objectstatic public double getArea(object Shape){// Pattern matching in Switch Case Statements// Variables also declared after casting in Case Statementswitch (Shape){case Rectangle r:return r.Length * r.Height;case Circle c:return 3.142 * c.Radius * c.Radius;}return 0;}static public double getPerimeter(object Shape){// Pattern matching in Switch Case Statements// Variables also declared after casting in Case Statementswitch (Shape){case Rectangle r:return r.Length * 2 + r.Height * 2;case Circle c:return 2 * 3.142 * c.Radius;}return 0;}static void Main(){// Declaring variablesCircle c = new Circle();c.Radius = 5;Rectangle r = new Rectangle();r.Height = 3;r.Length = 4;// The same function is called for both variablesSystem.Console.WriteLine(getArea(r));System.Console.WriteLine(getArea(c));System.Console.WriteLine(getPerimeter(r));System.Console.WriteLine(getPerimeter(c));}}
This program uses switch-case
statements to match variables to their correct types. The case statement then casts the object into the appropriate type. With this, the correct areas and perimeters are returned for the respective shapes.
Both methods provide the same outcome. It is up to the programmer to choose which of these they prefer or which is more suited to a particular problem.
Free Resources