[英]Case/Switch Statements in C#
我想知道是否有一种方法可以声明很多case语句而不必全部编写。 例如,我的代码:
switch (Weight)
{
case 0 to 2;
ShippingCost = "3.69";
case 3 to 4;
ShippingCost = "4.86";
case 5 to 6;
ShippingCost = "5.63";
case 7 to 8;
ShippingCost = "5.98";
case 9 to 10;
ShippingCost = "6.28";
case 11 to 30;
ShippingCost = "15.72";
}
我开始将VB转换为C#,并意识到要拥有多个case语句,必须声明它们。 如您所见,我有11到30,并且不想拥有所有这些行。
您不能像在VB中那样在C#中使用比较。 但是,您可以使用掉落情况,例如:
case 0:
case 1:
case 2:
ShippingCost = "3.69";
break;
case 3:
case 4:
ShippingCost = "4.86";
break;
请注意,非空情况需要throw
, return
或break
语句。 另请注意,您只能在空箱情况下失败。
编辑:
为完整性起见,正如其他人指出的那样,在这种情况下使用一系列if
语句可能更明智,例如:
if(Weight<=2) {
ShippingCost = "3.69";
}
else if(Weight <= 4) {
ShippingCost = "4.86";
}
... etc
在C#中有直接等效项,但是,您可以使用穿透(fall-through),因此不必重复执行:
switch (Weight)
{
case 0:
case 1:
case 2:
ShippingCost = "3.69";
break;
...
在这种情况下,如果语句可能更适合您:
if(Weight >= 0 && Weight <= 2){
ShippingCost = "3.69";
} else if(Weight >= 3 && Weight <= 4){
ShippingCost = "4.86";
}
...
像这样尝试:此解决方案无需在else if
语句中写&&
if(Weight >= 11 && Weight <= 30)
{
ShippingCost = "15.72";
}
else if(Weight >= 9)
{
ShippingCost = "6.28";
}
else if(Weight >= 7)
{
ShippingCost = "5.98";
}
else if(Weight >= 5)
{
ShippingCost = "5.63";
}
else if(Weight >= 3)
{
ShippingCost = "4.86";
}
else if(Weight >= 0)
{
ShippingCost = "3.69";
}
您也可以将其编写为Linq one衬纸:
var ShippingCost = (new[] { new { w = 2, p = "3,69" },
new { w = 4, p = "4.86" },
new { w = 6, p = "5.63" },
new { w = 8, p = "5.98" },
new { w = 10, p = "6.28" },
new { w = 30, p = "15.72" }})
.First(x => Weight <= x.w).p;
正如其他人已经指出的那样,您希望确保重量超过30的物品的运输也得到正确处理。
如何使用这种方法
private static double GetShippingCost(double weight)
{
if (weight > 30) throw new ArgumentException("Weight over allowed maximum", "weight");
if (weight <= 2) return 3.69;
if (weight <= 4) return 4.86;
if (weight <= 6) return 5.63;
if (weight <= 8) return 5.98;
if (weight <= 10) return 6.28;
if (weight <= 30) return 15.72;
}
使用case
的替代方法是编写某种类来进行映射,例如:
public sealed class CostsPerWeight
{
class CostPerWeight
{
public int Low;
public int High;
public double Cost;
}
readonly List<CostPerWeight> costs = new List<CostPerWeight>();
public CostsPerWeight Add(int low, int high, double result)
{
// Error handling omitted for brevity.
// Real code should check that low < high and that ranges do not overlap.
costs.Add(new CostPerWeight { Low = low, High = high, Cost = result } );
return this;
}
public double Cost(int weight)
{
// This throws if the weight isn't in the list.
// If that's not what you want, you'd have to add extra error handling here.
return costs.First(x => x.Low <= weight && weight <= x.High).Cost;
}
}
您将这样使用(在此示例中,我使用双精度代替字符串代替了字符串,但是您可以使用所需的任何类型):
var costs = new CostsPerWeight()
.Add( 0, 2, 3.69)
.Add( 3, 4, 4.86)
.Add( 5, 6, 5.63)
.Add( 7, 8, 5.98)
.Add( 9, 10, 6.28)
.Add(11, 30, 15.72);
double shippingCost = costs.Cost(weight);
如果VB中有很多这样的switch语句,那么值得考虑使用这种方法。
(使用此代码而不是Linq单行代码的好处是,它更易于记录和进行单元测试。您还可以创建CostsPerWeight
类实例并将其传递给CostsPerWeight
-对于解耦代码,依赖性注入和单元测试非常有用。 )
在我看来,基于权重查找成本的概念正在被要求封装在类中,而不是零碎地嵌入到代码的各个部分中。
这是CostsPerWeight
扩展示例,具有更多错误处理:
public class CostsPerWeight
{
class CostPerWeight
{
public int Low;
public int High;
public double Cost;
}
readonly List<CostPerWeight> costs = new List<CostPerWeight>();
double min = double.MaxValue;
double max = double.MinValue;
double costForMin;
public CostsPerWeight Add(int low, int high, double cost)
{
if (low > high)
throw new ArgumentException(nameof(low) + " must be less than " + nameof(high));
if (cost < 0)
throw new ArgumentOutOfRangeException(nameof(cost), "cost must be greater than zero");
costs.Add(new CostPerWeight { Low = low, High = high, Cost = cost } );
if (low < min)
{
min = low;
costForMin = cost;
}
if (high > max)
max = high;
return this;
}
public double Cost(int weight)
{
if (weight < min)
return costForMin;
if (weight > max)
throw new InvalidOperationException($"Weight {weight} is out of range: Must be <= {max}");
return costs.First(x => x.Low <= weight && weight <= x.High).Cost;
}
}
最清晰的方法是将数据放入对象中。
private struct ShippingCost
{
public int MinWeight;
public int MaxWeight;
public decimal Cost;
public ShippingCost(int min, int max, decimal cost)
{
MinWeight = min;
MaxWeight = max;
Cost = cost;
}
}
private List<ShippingCost> Costs = new List<ShippingCost>
{
new ShippingCost(0, 2, 3.69m),
new ShippingCost(3, 4, 4.86m),
new ShippingCost(5, 6, 5.63m),
new ShippingCost(7, 8, 5.98m),
new ShippingCost(9, 10, 6.28m),
new ShippingCost(11, 30, 15.72m),
};
// Choose shipping cost
public decimal CalcShippingCost(int weight)
{
foreach (ShippingCost sc in Costs)
{
if (weight >= sc.MinWeight && weight <= sc.MaxWeight)
return sc.Cost;
}
return 0.00m; // default cost
}
您无法在C#中做到这一点。 如果您的体重最大值是30,最好的选择是使用默认情况。
否则,如果您不想要类似
case 11:
case 12:
case 13:
....
case 28:
case 29:
case 30:
“老式” if / else if将是最易读的解决方案
我建议将数据存储在容器中并遍历该容器。 您可以创建自己的类或使用诸如Tuple的.net类:
var shippingCostsByWeight = new List<Tuple<int, int, string>>
{
new Tuple<int, int, string>(0, 2, "3.69"),
new Tuple<int, int, string>(3, 4, "4.86"),
new Tuple<int, int, string>(5, 6, "5.63"),
new Tuple<int, int, string>(7, 8, "5.98"),
new Tuple<int, int, string>(9, 10, "6.28"),
new Tuple<int, int, string>(11, 30, "15.72"),
};
ShippingCost = shippingCostsByWeight
.First(tuple => weight >= tuple.Item1 && weight <= tuple.Item2).Item3;
您可以使用以下情况切换语句:
case 0:
case 1:
case 2:
shippingCost = "3.69";
break;
... and so on
这将导致0将shippingCost设置为3.69以及1和2。:)
那就是我的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.