C# Switch模式匹配语法

创建3个类

 1 class Circle
 2     {
 3         private double _radius;
 4 
 5         public Circle(double radius) => _radius = radius;
 6 
 7 
 8         public double ComputerArea1()
 9         {
10             return _radius * _radius * Math.PI;
11         }
12     }
13 
14 
15     class Rectangle
16     {
17 
18         private double _width;
19 
20         private double _height;
21 
22         public Rectangle(double width, double height)
23         {
24             _width = width;
25             _height = height;
26         }
27 
28         public double ComputerArea2()
29         {
30             return _width * _height;
31         }
32     }
33 
34 
35     class Triangle
36     {
37 
38         private double _bottom;
39 
40         private double _height;
41 
42         public Triangle(double bottom, double height)
43         {
44             _bottom = bottom;
45             _height = height;
46         }
47 
48         public double ComputerArea3()
49         {
50             return _bottom * _height / 2;
51         }
52     }

创建 计算类 常规写法

 1 class Calculate
 2     {
 3         public void Calc(object obj)
 4         {
 5             //常规写法
 6             if (obj is Circle c)
 7             {
 8 
 9                 Console.WriteLine($"{c.GetType().Name}的面积:{c.ComputerArea1()}");
10             }
11             else if (obj is Rectangle r)
12             {
13                 Console.WriteLine($"{r.GetType().Name}的面积:{r.ComputerArea2()}");
14 
15             }
16             else if (obj is Triangle t)
17             {
18                 Console.WriteLine($"{t.GetType().Name}的面积:{t.ComputerArea3()}");
19             }
20             else
21             {
22                 Console.WriteLine("没定义的图形");
23             }
24         }
25     }

运行测试

 1 static void Main(string[] args)
 2         {
 3             var circle = new Circle(5);
 4             var rect = new Rectangle(5, 10);
 5             var triangle = new Triangle(6, 8);
 6 
 7             var calc = new Calculate();
 8 
 9             calc.Calc(circle);
10             calc.Calc(rect);
11             calc.Calc(triangle);
12 
13         }

运行结果

 

更改计算类的方法的写法

 1 public void Calc(object obj)
 2         {
 3 
 4             #region 常规写法
 5             //if (obj is Circle c)
 6             //{
 7 
 8             //    Console.WriteLine($"{c.GetType().Name}的面积:{c.ComputerArea1()}");
 9             //}
10             //else if (obj is Rectangle r)
11             //{
12             //    Console.WriteLine($"{r.GetType().Name}的面积:{r.ComputerArea2()}");
13 
14             //}
15             //else if (obj is Triangle t)
16             //{
17             //    Console.WriteLine($"{t.GetType().Name}的面积:{t.ComputerArea3()}");
18             //}
19             //else
20             //{
21             //    Console.WriteLine("没定义的图形");
22             //}
23             #endregion
24             #region 模式匹配写法
25             switch (obj)
26             {
27                 case Circle c:
28                     Console.WriteLine($"{c.GetType().Name}的面积:{c.ComputerArea1()}");
29                     break;
30                 case Rectangle r:
31                     Console.WriteLine($"{r.GetType().Name}的面积:{r.ComputerArea2()}");
32                     break;
33                 case Triangle t:
34                     Console.WriteLine($"{t.GetType().Name}的面积:{t.ComputerArea3()}");
35                     break;
36                 default:
37                     break;
38             }
39 
40             #endregion
41         }

运行结果 

 

对类更改一下,封装下字段

 1 class Circle
 2     {
 3         private double _radius;
 4         public double Radius { get => _radius; set => _radius = value; }
 5 
 6 
 7         public Circle(double radius) => Radius = radius;
 8 
 9 
10         public double ComputerArea1()
11         {
12             return Radius * Radius * Math.PI;
13         }
14     }
15 
16 
17     class Rectangle
18     {
19 
20         private double _width;
21         public double Width { get => _width; set => _width = value; }
22 
23         private double _height;
24         public double Height { get => _height; set => _height = value; }
25 
26 
27         public Rectangle(double width, double height)
28         {
29             Width = width;
30             Height = height;
31         }
32 
33 
34         public double ComputerArea2()
35         {
36             return Width * Height;
37         }
38     }
39 
40 
41     class Triangle
42     {
43 
44         private double _bottom;
45         public double Bottom { get => _bottom; set => _bottom = value; }
46 
47         private double _height;
48         public double Height { get => _height; set => _height = value; }
49 
50 
51         public Triangle(double bottom, double height)
52         {
53             Bottom = bottom;
54             Height = height;
55         }
56 
57 
58         public double ComputerArea3()
59         {
60             return Bottom * Height / 2;
61         }
62     }

更改下计算类的方法写法

 1 public void Calc(object obj)
 2         {
 3 
 4             #region 常规写法
 5             //if (obj is Circle c)
 6             //{
 7 
 8             //    Console.WriteLine($"{c.GetType().Name}的面积:{c.ComputerArea1()}");
 9             //}
10             //else if (obj is Rectangle r)
11             //{
12             //    Console.WriteLine($"{r.GetType().Name}的面积:{r.ComputerArea2()}");
13 
14             //}
15             //else if (obj is Triangle t)
16             //{
17             //    Console.WriteLine($"{t.GetType().Name}的面积:{t.ComputerArea3()}");
18             //}
19             //else
20             //{
21             //    Console.WriteLine("没定义的图形");
22             //}
23             #endregion
24             #region 模式匹配写法
25             switch (obj)
26             {
27                 //当半径大于10的时候
28                 case Circle c when c.Radius > 10:
29                     Console.WriteLine("半径大于10");
30                     break;
31                 //正常情况
32                 case Circle c:
33                     Console.WriteLine($"{c.GetType().Name}的面积:{c.ComputerArea1()}");
34                     break;
35                 case Rectangle r:
36                     Console.WriteLine($"{r.GetType().Name}的面积:{r.ComputerArea2()}");
37                     break;
38                 case Triangle t:
39                     Console.WriteLine($"{t.GetType().Name}的面积:{t.ComputerArea3()}");
40                     break;
41                 default:
42                     break;
43             }
44 
45             #endregion
46         }

测试运行

 1  static void Main(string[] args)
 2         {
 3             var circle1 = new Circle(5);
 4             var circle2 = new Circle(15);
 5 
 6             var rect = new Rectangle(5, 10);
 7             var triangle = new Triangle(6, 8);
 8             Triangle nul = null;
 9 
10             var calc = new Calculate();
11 
12             calc.Calc(circle1);
13             calc.Calc(circle2);
14             calc.Calc(rect);
15             calc.Calc(triangle);
16             calc.Calc(nul);
17 
18         }

运行结果 

 另外还可以 匹配 var 或者null 具体用法可以查看微软文档 模式匹配 - C# 指南 | Microsoft Docs

这个只是7.0的应用,8.0和9.0 功能更强大!

posted @ 2021-05-01 10:24  只吃肉不喝酒  阅读(1501)  评论(1编辑  收藏  举报