C# 关于循环/开关语句的适当性。如何在switch语句中使用arrayList?

C# 关于循环/开关语句的适当性。如何在switch语句中使用arrayList?,c#,if-statement,arraylist,switch-statement,refactoring,C#,If Statement,Arraylist,Switch Statement,Refactoring,第一个问题是关于将IF语句保持原样或将其更改为switch语句是否是更好的编程实践 public Booking bookVehicle(String vehicleClass, Client potentialClient, int number) { ArrayList vehicles; if (vehicleClass.Equals("a", StringComparison.OrdinalIgnoreCase)) veh

第一个问题是关于将IF语句保持原样或将其更改为switch语句是否是更好的编程实践

 public Booking bookVehicle(String vehicleClass, Client potentialClient, int number)
    {
        ArrayList vehicles;
        if (vehicleClass.Equals("a", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Prestige Sedan");
        else if (vehicleClass.Equals("b", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Luxury Sedan");
        else if (vehicleClass.Equals("c", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Full-size Sedan");
        else if (vehicleClass.Equals("d", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
        else if (vehicleClass.Equals("e", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Hatchback");
        else if (vehicleClass.Equals("f", StringComparison.OrdinalIgnoreCase))
            vehicles = thisLocation.getVehicleClass("Compact");
        else vehicles = thisLocation.getVehicleClass("Compact");

        if (available(vehicles, number))
        {
            ArrayList hiredVehicles = new ArrayList();
            int currentVehicle = 0;
            int hireCharge = 0;
            while (number > 0)
            {
                Vehicle thisVehicle = (Vehicle)vehicles[currentVehicle];
                if (!thisVehicle.hired)
                {
                    hiredVehicles.Add(thisVehicle);
                    hireCharge += thisVehicle.hireFee;
                    number--;
                }
                currentVehicle++;
            }
            return new Booking(potentialClient, hireCharge, hiredVehicles);
        }
        else
        {
            return new Booking(potentialClient, false);
        }
    }
因此,上面的代码正在评估车辆的ArrayList,车辆的属性之一是“Vehicle Class”,通过使用IF语句。使用Switch语句为什么/为什么不更好

下面是我的代码,试图实现与上面相同的功能,但使用switch语句,我打算将其放入一个单独的方法中,该方法将被调用,但我不知道如何使用arrayList

    switch (ArrayList vehicles)
{
    case 'a':
        vehicles = thislocation.getVehicleClass("prestige Sedan");
        break;
    case 'b':
        vehicles = thisLocation.getVehicleClass("Luxury Sedan");
        break;
    case 'c':
        vehicles = thisLocation.getVehicleClass("Full-size Sedan");
        break;
    case 'd':
        vehicles = thisLocation.getVehicleClass("Mid-size Sedan");
        break;
    case 'e':
        vehicles = thisLocation.getVehicleClass("Hatchback");
        break;
    case 'f':
        vehicles = thisLocation.getVehicleClass("Compact");
        break;
        default:
    vehicles = thisLocation.getVehicleClass("Compact");
        break;
return vehicles;
}

如果您能帮助我使这个switch语句与If语句(如果可能/适当)具有相同的功能,我将不胜感激。

switch语句指定要测试其值的变量

您正在测试字符串的值,而不是ArrayList:

switch (String vehicleClass)

至于
开关
块是否适合
if/else
块,这是一个意见问题。使用易于阅读和维护的选项。

有一种方法可以避免切换/if。拥有一个字典,其中包含字符串a、b、c、d、e的键以及返回所需车辆类别列表的ArrayList条目,大致如下:

Dictionary<string, ArrayList> vehicleDict = { 
{"a", thisLocation.getVehicleClass("whatever"}, 
{...}, 
{"e", thisLocation.getVehicleClass("some other class")} 
};
Dictionary vehicleDict={
{“a”,thisLocation.getVehicleClass(“无论什么”},
{...}, 
{“e”,thisLocation.getVehicleClass(“其他一些类”)}
};
然后,该方法的初始部分变为
ArrayList vehicles=vehicleDict[vehicleClass];

注1:使用枚举而不是字符串将使这一点更好

注2:如果这不能编译,很可能是因为我在手机上打字


注3:我从来没有打过case;我相信你能做到。当然,你可以把字典放在有意义的地方。

好的,谢谢。在这种情况下使用switch语句会更好吗?是的,好的,我明白了,这是一个重构作业,所以我觉得可能需要修改它,只是为了阅读的角度。这也是一个相当大的问题很长的一段,所以我认为应该把它分解成更小的方法。