What is pattern matching in C# 7.0?

WBOY
Release: 2023-09-17 12:09:03
forward
1487 people have browsed it

C# 7.0 中的模式匹配是什么?

C# 7.0 introduces pattern matching in two situations: is expressions and switch statement.

Pattern tests whether a value has a certain shape and can be obtained from The value when having a matching shape.

Pattern matching provides a cleaner syntax for algorithms

You can perform pattern matching on any data type (even your own data type), and If/else, you always need primitives to match.

Pattern matching can extract values ​​from expressions.

Before pattern match -

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();
   }
}
Copy after login

Output

Area of Rect 402.59999999999997
Area of Rect 536.8
Area of Circle 452.44799423217773
Copy after login
Copy after login

After pattern match -

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();
   }
}
Copy after login

Output

Area of Rect 402.59999999999997
Area of Rect 536.8
Area of Circle 452.44799423217773
Copy after login
Copy after login

The above is the detailed content of What is pattern matching in C# 7.0?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template