我是C#的新手,來自Java,語法有點不同。 我想返回一個arrayList中元素的雙精度值。我想返回ArrayList中元素的雙重屬性
public ArrayList vehicleList { get; set; }
class Vehicle
{
//A vehicle can be a car and must have an ID, a price and a licenseplate number.
public int iD { get; set; }
public double price { get; set; }
public string licensePlate { get; set; }
public typeVehicle type;
//Constructor of vehicle, type of vehicle will be parsed from string to enum.
public Vehicle(int iD, string typeName, double price, string license)
{
this.iD = iD;
this.type = (typeVehicle)Enum.Parse(typeof(typeVehicle), typeName);
}
}
//Get the price of the vehicle with parameter ID.
public double getPriceVehicle(int iD)
{
if (iD >= 0 && iD < vehicleList.Count)
{
foreach (Vehicle vehicle in vehicleList)
{
return vehicleList.Contains(iD).price;
}
}
}
的問題是,我上回線得到一個錯誤,我不能在網上找到任何解決方案這完全是。任何幫助,將不勝感激。
謝謝大家在這篇文章上的回覆。我最終選擇了最簡單的解決方案,因爲那是我理解的那個。使用LINQ的答案我也理解,但這是我在課程中不需要學習的東西。 我故意讓iD和陣列中的位置相同,因爲現在它更容易,這也意味着第一輛車將具有00的iD。 – Aleathia