我有以下代码:
var currentCity = db.City.Find(player.Location);
switch (TravelTo) {
case 1:
costs = currentCity.TravelCost.City1;
break;
case 2:
costs = currentCity.TravelCost.City2;
break;
case 3:
costs = currentCity.TravelCost.City3;
break;
case 4:
costs = currentCity.TravelCost.City4;
break;
case 5:
costs = currentCity.TravelCost.City5;
break;
}
有没有更好的方法来获得成本价值?有没有办法可以更改我要调用的字符串的名称?所以我可以创造类似的东西
costs = currentCity.TravelCost.City[TravelTo]
或类似的东西?我觉得我写了一个不必要的 switch 语句,但我对 C# 的了解还不足以想出一个替代方案。
谢谢,