1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public record Point(int X, int Y);
static string DescribePosition(Point p) => p switch { (0, 0) => "原点", (0, _) => "Y 轴上", (_, 0) => "X 轴上", var (x, y) when x == y => "对角线上", _ => $"({p.X}, {p.Y})" };
static string DescribeList(int[] numbers) => numbers switch { [] => "空数组", [var first] => $"单个元素: {first}", [var first, var second] => $"两个元素: {first}, {second}", [_, _, _, ..] => "至少三个元素", _ => "其他" };
record Product(string Name, decimal Price, bool IsAvailable);
static decimal CalculateDiscount(Product product) => product switch { { IsAvailable: false } => 0, { Price: > 1000, Name: var name } => product.Price * 0.15m, { Price: > 500 } => product.Price * 0.1m, _ => product.Price * 0.05m, };
|