具有关系模式的 C# 9 新 "nested switch expression"
C# 9 new "nested switch expression" with relational patterns
在https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/#relational-patterns中有一个使用“嵌套开关表达式”的例子:
DeliveryTruck t when t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
而不是:
DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,
DeliveryTruck t when t.GrossWeightClass < 3000 => 10.00m - 2.00m,
DeliveryTruck _ => 10.00m,
但我无法让它工作...我的完整代码:
public class DeliveryTruck {
public int GrossWeightClass { get; set; }
}
public class Class1 {
public decimal CalculateTollOriginal(object vehicle) =>
vehicle switch
{
DeliveryTruck t when (t.GrossWeightClass > 5000) => 10.00m + 5.00m,
DeliveryTruck t when (t.GrossWeightClass < 3000) => 10.00m - 2.00m,
DeliveryTruck t => 10.00m,
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};
public decimal CalculateTollNestedSwitch(object vehicle) =>
vehicle switch
{
DeliveryTruck t when t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};
}
我在使用 dotnet 5.0.100 时遇到编译错误:
C:\Users\pkruk\source\repos\CSharp9\PatternMatching2_NestedSwitch.cs(29,14): error CS1003: Syntax error, '=>' expected [C:\Users\pkruk\source\repos\CSharp9\CSharp9.csproj]
C:\Users\pkruk\source\repos\CSharp9\PatternMatching2_NestedSwitch.cs(29,14): error CS1525: Invalid expression term ',' [C:\Users\pkruk\source\repos\CSharp9\CSharp9.csproj]
我做错了吗?
您需要的是
public decimal CalculateTollNestedSwitch(object vehicle) => vehicle switch
{
DeliveryTruck t => t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};
在https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/#relational-patterns中有一个使用“嵌套开关表达式”的例子:
DeliveryTruck t when t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
而不是:
DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,
DeliveryTruck t when t.GrossWeightClass < 3000 => 10.00m - 2.00m,
DeliveryTruck _ => 10.00m,
但我无法让它工作...我的完整代码:
public class DeliveryTruck {
public int GrossWeightClass { get; set; }
}
public class Class1 {
public decimal CalculateTollOriginal(object vehicle) =>
vehicle switch
{
DeliveryTruck t when (t.GrossWeightClass > 5000) => 10.00m + 5.00m,
DeliveryTruck t when (t.GrossWeightClass < 3000) => 10.00m - 2.00m,
DeliveryTruck t => 10.00m,
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};
public decimal CalculateTollNestedSwitch(object vehicle) =>
vehicle switch
{
DeliveryTruck t when t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};
}
我在使用 dotnet 5.0.100 时遇到编译错误:
C:\Users\pkruk\source\repos\CSharp9\PatternMatching2_NestedSwitch.cs(29,14): error CS1003: Syntax error, '=>' expected [C:\Users\pkruk\source\repos\CSharp9\CSharp9.csproj]
C:\Users\pkruk\source\repos\CSharp9\PatternMatching2_NestedSwitch.cs(29,14): error CS1525: Invalid expression term ',' [C:\Users\pkruk\source\repos\CSharp9\CSharp9.csproj]
我做错了吗?
您需要的是
public decimal CalculateTollNestedSwitch(object vehicle) => vehicle switch
{
DeliveryTruck t => t.GrossWeightClass switch
{
> 5000 => 10.00m + 5.00m,
< 3000 => 10.00m - 2.00m,
_ => 10.00m,
},
{ } => throw new System.ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)),
null => throw new System.ArgumentNullException(nameof(vehicle))
};