List<Vehicle> vehicleList { get; set; }
//Get the price of the vehicle with parameter ID.
public double GetPriceVehicle(int id)
{
var result = vehicleList.FirstOrDefault(v => v.ID == id);
if (result == null)
{
throw new NotImplementedException(String.Format("Vehicle with ID={0} was not found", id)); //todo: put in the logic you want for when no vehicle has the given id
}
return result.Price;
}
1) Use a Generic List of type Vehicle (System.Collections.Generic.List<Vehicle>
) instead of an ArrayList
. See here for more: ArrayList vs List<> in C#
2) You're using ID as both an ID and an Index. i.e. An ID is a value assigned to a specific vehicle, which should remain the same regardless of whether you add or remove other vehicles; i.e. it's an identifier. An Index is the number of an item in the list. To give an example, if I'm in a queue of cars at a drive through and the car at the front of the queue pulls away then by them leaving my index changes; i.e. I go from being 5th in the queue to 4th. On the other hand, if I'm talking to my insurance company and they say my car's on file as #3265, should they lose a customer I wouldn't expect them to write to me saying that my ID's now #3264, and that all my documentation with this reference number would now need to be updated.
3) The Where
logic here uses something called a lambda expression; i.e. we're searching through all vehicles in the list for one which has an ID which matches the ID passed into the function. This lambda expression could be placed in an actual Where
statement (e.g. Where(v => v.ID == id).FirstOrDefault()
), but FirstOrDefault
allows us to include the lambda expression in it directly anyway.
https://msdn.microsoft.com/en-us/library/bb397687.aspx
4) The FirstOrDefault
says that if we find a result we stop searching (i.e. we're not expecting a second vehicle on the same ID, so don't waste time looking after we've found one). The Default part of FirstOrDefault says if we don't find a match, return the default value for this type; default(Vehicle)
; which given Vehicle is a reference type (i.e. a class), is null.
https://msdn.microsoft.com/pt-br/library/bb909042(v=vs.90).aspx
5) If result is null we can't return result.Price
; that would error as a null object has no properties. An error's fine; but not necessarily that helpful; better to throw an error of our own detailing the issue; we can then add logic to handle that error in a sensible way in any code calling this method.
https://msdn.microsoft.com/en-us/library/ms173163.aspx
6) I also changed some of the variable/property names; e.g. iD
to ID
. Generally anything public should be in pascalCase; anything private/local should be in camelCase. https://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx
7) I didn't implement it here, but a better option may be a generic Dictionary (Dictionary<long, Vehicle>
); as this data structure is optimised for fast retrieval, so rather than searching through each item in a list to find a match, uses a hashtable to quickly find the data associated with the given key/ID.
http://geekswithblogs.net/blackrabbitcoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx