P.B*_*key 0 c# language-specifications
我有两个引用类型之间的显式转换设置.
class Car
{
public void Foo(Car car)
{
}
public static explicit operator Bike(Car car)
{
return new Bike();
}
}
class Bike
{
}
Run Code Online (Sandbox Code Playgroud)
如果我调用Foo并传递一种类型Bike,那么我必须执行显式转换.
Car myCar = new Car();
Bike bike = (Bike)myCar;
myCar.Foo(bike);//Error: Cannot convert from Bike to Car.
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加扩展方法,则不再需要显式转换.
public static void Foo(this Car car, Bike bike)
{
car.Foo(bike);//Success...
}
Run Code Online (Sandbox Code Playgroud)
为什么扩展方法能够使用Bike隐式类型调用Foo ?
为什么扩展方法能够隐式调用类型为Bike的Foo?
不是.您提供的代码会导致编译器错误.
至少有以下一种情况属实:
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |