2

I am getting the following error:

enter image description here

Type of conditional expression cannot be determined because there is no implicit conversion between 'Car' and 'Bike'

public interface IVehicle
{
    int Wheels { get; }
}

public class Car : IVehicle
{
    public int Wheels => 4;
}

public class Bike : IVehicle
{
    public int Wheels => 2;
}

public Garage()
{
    var licenseHeld = false;
    IVehicle vehicle = licenseHeld ? new Car() : new Bike();
}


Why cannot this conversion be done when both Car and Bike implement the same interface?

MightyAtom
  • 331
  • 4
  • 24

1 Answers1

1

The type of consequent and alternative must be the same, or there must be an implicit conversion from one type to the other.

https://learn.microsoft.com/ru-ru/dotnet/csharp/language-reference/operators/conditional-operator

progpow
  • 1,560
  • 13
  • 26