Skip to content

Instantly share code, notes, and snippets.

@ajryan
Created September 9, 2024 13:21
Show Gist options
  • Save ajryan/43554dede141750ed2d1084c4eca0c4b to your computer and use it in GitHub Desktop.
Save ajryan/43554dede141750ed2d1084c4eca0c4b to your computer and use it in GitHub Desktop.
Don't accept IEnumerable
public class Vehicle
{
public required string Make { get; init; }
public required string Model { get; init; }
public required int Year { get; init; }
public required decimal Price { get; init; }
}
public class BadVehicleProcessor
{
private IEnumerable<Vehicle> vehicles;
public BadVehicleProcessor(IEnumerable<Vehicle> vehicles)
{
this.vehicles = vehicles;
}
public Vehicle? GetOldestVehicle()
{
return this.vehicles.OrderBy(v => v.Year).FirstOrDefault();
}
public Vehicle? GetMostExpensiveVehicle()
{
return this.vehicles.OrderByDescending(v => v.Price).FirstOrDefault();
}
}
public class GoodVehicleProcessor
{
private IReadOnlyCollection<Vehicle> vehicles;
public BadVehicleProcessor(IReadOnlyCollection<Vehicle> vehicles)
{
this.vehicles = vehicles;
}
// Identical implementation to BadVehicleProcessor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment