I have an explicit conversion setup between two reference types.
class Car
{
public void Foo(Car car)
{
}
public static explicit operator Bike(Car car)
{
return new Bike();
}
}
class Bike
{
}
If I invoke Foo and pass a type of Bike
, then I must perform an explicit conversion.
Car myCar = new Car();
Bike bike = (Bike)myCar;
myCar.Foo(bike);//Error: Cannot convert from Bike to Car.
However, if I add an extension method, then explicit conversio is no longer required.
public static void Foo(this Car car, Bike bike)
{
car.Foo(bike);//Success...
}
Why is the extension method able to invoke Foo with a type of Bike
implicitly?