Last active
October 6, 2021 01:15
-
-
Save relyky/e45f5daecd6e2b2f3f89be7e43221db3 to your computer and use it in GitHub Desktop.
C# switch expression, pattern matching 語法整理, ref→[The evolution of Pattern Matching in C# (from version 6 to 10)](https://www.youtube.com/watch?v=MzNHMJCyU40&ab_channel=NickChapsas)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// | |
| /// C# switch pattern 語法整理 | |
| /// ref→[The evolution of Pattern Matching in C# (from version 6 to 10)](https://www.youtube.com/watch?v=MzNHMJCyU40&ab_channel=NickChapsas) | |
| /// ref→[Pattern matching overview](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching) | |
| /// ref→[Patterns (C# reference)](https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/operators/patterns) | |
| /// | |
| using System; | |
| using System.Collections.Generic; | |
| namespace ConsoleApp2019 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var circle = new Circle(5); | |
| var rectangle = new Rectangle(420, 1337); | |
| var square = new Rectangle(69, 69); | |
| var shapes = new List<Shape> { circle, rectangle, square }; | |
| var randomShape = shapes[new Random().Next(shapes.Count)]; | |
| //## Let's go | |
| #region with C#6 | |
| { | |
| if (randomShape is Circle) | |
| { | |
| var cir6 = (Circle)randomShape; | |
| Console.WriteLine($"Circle with area {cir6.Area}"); | |
| } | |
| } | |
| #endregion | |
| #region with C#7 | |
| { | |
| if (randomShape is Circle cir7) | |
| { | |
| Console.WriteLine($"Circle with area {cir7.Area}"); | |
| } | |
| switch (randomShape) | |
| { | |
| case Circle c: | |
| Console.WriteLine($"這是個圓形其面積有{c.Area}。"); | |
| break; | |
| case Rectangle r when r.Height == r.Width: | |
| Console.WriteLine($"這是個正方形其面積有{r.Area}"); | |
| break; | |
| default: | |
| Console.WriteLine($"沒什麼特別的形狀。"); | |
| break; | |
| } | |
| } | |
| #endregion | |
| #region with C#8 | |
| { | |
| if (randomShape is Circle { Radius: 10, Area: 50 }) | |
| { | |
| } | |
| string shapeDetaisl = randomShape switch | |
| { | |
| Circle cir | |
| => $"這是個圓形其面積有{cir.Area}。", | |
| Rectangle rect when rect.Height == rect.Width | |
| => $"這是個正方形其面積有{rect.Area}", | |
| { Area: 100 } | |
| => $"此面積有100。", | |
| { Area: > 100 } | |
| => $"此面積大於100。", | |
| _ => "什麼特性都沒有。" | |
| }; | |
| Console.WriteLine(shapeDetaisl); | |
| } | |
| #endregion | |
| #region with C#9 | |
| { | |
| if (randomShape is not Rectangle) | |
| { | |
| } | |
| if (randomShape is Circle { Radius: > 100 and < 200, Area: >= 1000 }) | |
| { | |
| } | |
| if (randomShape is Rectangle { ShapeInShape: { Area: 100 } }) | |
| { | |
| } | |
| string shapeDetaisl = randomShape switch | |
| { | |
| Circle { Area: > 100 and < 200 } => "一個神奇的圓形。", | |
| Circle cir => $"這是個圓形其面積有{cir.Area}。", | |
| Rectangle rect when rect.Height == rect.Width | |
| => $"這是個正方形其面積有{rect.Area}", | |
| { Area: 100 } | |
| => $"此面積有100。", | |
| { Area: > 100 } | |
| => $"此面積大於100。", | |
| _ => "什麼特性都沒有。" | |
| }; | |
| var areaDetails = randomShape.Area switch | |
| { | |
| >= 100 and <= 200 => "...", | |
| _ => "not match" | |
| }; | |
| Console.WriteLine(shapeDetaisl); | |
| } | |
| #endregion | |
| Console.WriteLine("Press any key to continue."); | |
| Console.ReadKey(); | |
| } | |
| } | |
| public abstract class Shape | |
| { | |
| public abstract double Area { get; } | |
| public Shape ShapeInShape { get; set; } | |
| } | |
| public interface ISquare | |
| { | |
| double Height { get; set; } | |
| double Width { get; set; } | |
| } | |
| public class Rectangle : Shape, ISquare | |
| { | |
| public Rectangle(double height, double width) | |
| { | |
| Height = height; | |
| Width = width; | |
| } | |
| public double Height { get; set; } | |
| public double Width { get; set; } | |
| public override double Area => Height * Width; | |
| } | |
| public class Circle : Shape | |
| { | |
| const double PI = Math.PI; | |
| public Circle(double diameter) | |
| { | |
| Diameter = diameter; | |
| } | |
| public double Diameter { get; set; } | |
| public double Radius => Diameter / 2.0; | |
| public override double Area => PI * Radius * Radius; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment