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         }

运行结果

C# Switch模式匹配语法

 

更改计算类的写法

 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         }

运行结果 

C# Switch模式匹配语法

上一篇:shengdanshu


下一篇:盒子居中方法 注:定位