https://www.geeksforgeeks.org/pattern-matching-in-c-sharp/
https://www.codeproject.com/Articles/5368148/Your-Quick-Guide-to-Pattern-Matching-in-Csharp
模式匹配是一种在代码中识别和提取数据的机制。它允许您以声明式的方式检查对象的形状,而无需编写冗长的条件语句。
int
、float
、char
、string
、bool
、,用const
、null
声明的字段。_
的模式匹配,匹配任何内容,包括 null
,它的用法在新的开关表达式中大放异彩,以匹配默认或否则情况。is
和 as
操作符,提供更简洁的语法。switch
语句中进行类型匹配。if
语句中进行属性模式匹配。var
模式中使用位置模式匹配。switch
语句中对列表进行模式匹配。try-catch
语句中使用模式匹配捕获特定类型的异常。switch
语句中对异常类型进行模式匹配。类型判断和转换:
object obj = "Hello, world!";
if (obj is string s)
{
Console.WriteLine(s.ToUpper()); // 输出: HELLO, WORLD!
}
switch (obj)
{
case string s:
Console.WriteLine($"It's a string: {s}");
break;
case int i:
Console.WriteLine($"It's an integer: {i}");
break;
default:
Console.WriteLine("It's something else");
break;
}
属性检查:
var person = new { Name = "Alice", Age = 30 };
if (person is { Age: >= 18 })
{
Console.WriteLine($"{person.Name} is an adult.");
}
数据解构:
var point = (10, 20);
if (point is (int x, int y))
{
Console.WriteLine($"X: {x}, Y: {y}");
}
var list = new List<int> { 1, 2, 3, 4, 5 };
if (list is [_, _, int middle, _, _])
{
Console.WriteLine($"The middle element is: {middle}");
}
列表和数组处理:
var numbers = new List<int> { 1, 2, 3, 4, 5 };
switch (numbers)
{
case []:
Console.WriteLine("The list is empty.");
break;
case [int first]:
Console.WriteLine($"The list has one element: {first}");
break;
case [int first, int second]:
Console.WriteLine($"The list has two elements: {first}, {second}");
break;
case var all:
Console.WriteLine($"The list has {all.Count} elements.");
break;
}
错误处理:
try
{
// 某些可能引发异常的操作
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"Caught DivideByZeroException: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Caught general Exception: {ex.Message}");
}
领域驱动设计:
public record Person(string Name, int Age);
var person = new Person("Alice", 30);
if (person is { Age: >= 18 })
{
Console.WriteLine($"{person.Name} is an adult.");
}
恒定模式:
public bool IsFreshProduce(FoodModel food)
{
return food?.Category?.ID is (int) ProductCategoryEnum.FreshProduce;
}
弃元模式:
//如果未提供食物的储存温度或数字与以下范围不匹配,则将引发自定义异常
public int GetFoodStorageTemperature
(StorageRequirementEnum storageRequirement) => storageRequirement switch
{
StorageRequirementEnum.Freezer => -18,
StorageRequirementEnum.Refrigerator => 4,
StorageRequirementEnum.RoomTemperature => 25,
_ => throw new InvalidStorageRequirementException()
};
// switch (storageRequirement)
// {
// case StorageRequirementEnum.Freezer:
// return -18;
// case StorageRequirementEnum.Refrigerator:
// return 4;
// case StorageRequirementEnum.RoomTemperature:
// return 25;
// default:
// throw new Exception();
// }
元组模式:
//转基因食品检测
public string GetFoodDescription(FoodModel food) => (food.NonGMO, food.Category.ID) switch
{
(true, (int)ProductCategoryEnum.FreshProduce) => "Non-GMO Fresh Product",
(true, (int)ProductCategoryEnum.Dairy) => "Non-GMO Dairy",
(false, (int)ProductCategoryEnum.Meats) => "GMO Meats. Avoid!",
(_, _) => "Invalid Food Group"
};
// switch (food.NonGMO, food.Category.ID)
// {
// case (true, (int)ProductCategoryEnum.FreshProduce):
// return "Non-GMO Fresh Product";
// case (true, (int)ProductCategoryEnum.Dairy):
// return "Non-GMO Dairy";
// case (false, (int)ProductCategoryEnum.Meats):
// return "GMO Meats. Avoid!";
// default:
// return "Invalid Food Group";
// }
列表模式:
public (int?, int?) FindNumberOneAndNumberFour()
{
int[] numbers = { 1, 2, 3, 4, 5 };
// Match if 2nd value is anything, 3rd is greater than or equal 3, fifth is 5
if (numbers is [var numberOne, _, >= 3, int numberFour, 5])
{
return (numberOne, numberFour);
}
return (null, null);
}
这些案例展示了模式匹配在各种场景下的应用,包括类型判断、属性检查、数据解构、列表处理、错误处理以及领域建模等。通过使用模式匹配,您可以编写更简洁、可读性更强的代码,同时也能在某些情况下提高性能。熟练掌握这些模式匹配的使用技巧,将有助于您提高代码质量和开发效率。
获得永久免费的无限 GPT 查询次数,点击链接并下载Monica插件,即可参加限时活动。
https://monica.im/invitation?c=OWBJ2ZBE