Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

How to use Pattern Matching in C#

Hassan Azhar

Pattern Matching

Pattern Matching can be used to extract information from a variable or to determine a variable’s type.

Usage

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 structs
  public 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 object
  static public double getArea(object Shape)
  {
    // Pattern matching in If Statements
    // Variables also declared after casting in If Statement
    if (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 Statement
    if (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 variables
    Circle c = new Circle();
    c.Radius = 5;
    Rectangle r = new Rectangle();
    r.Height = 3;
    r.Length = 4;
    
    // The same function is called for both variables
    System.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 structs
  public 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 object
  static public double getArea(object Shape)
  {
    // Pattern matching in Switch Case Statements
    // Variables also declared after casting in Case Statement
    switch (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 Statement
    switch (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 variables
    Circle c = new Circle();
    c.Radius = 5;
    Rectangle r = new Rectangle();
    r.Height = 3;
    r.Length = 4;

    // The same function is called for both variables
    System.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.

RELATED TAGS

c#

CONTRIBUTOR

Hassan Azhar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring