Vic*_*aci 3 c# interface optional-parameters
鉴于:
interface IFoo
{
void Print(string text = "abc");
}
class Bar : IFoo
{
public void Print(string text = "def")
{
Console.WriteLine(text);
}
}
class Program
{
static void Main(string[] args)
{
Bar b = new Bar();
b.Print();
IFoo f = b as IFoo;
f.Print();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
def
abc
Run Code Online (Sandbox Code Playgroud)
只是我还是这有点奇怪?最初我在两种情况下都期待"def".但是,如果是这种情况,则可选参数抽象方法将毫无用处.但似乎仍然是一个讨厌的错误的良好起点.
可选参数是编译器功能,编译器只能处理编译时可用的类型信息.因此,值取自您正在操作的引用类型,而不是实际的运行时类型.在这个简单的测试用例中,可以找出f通过静态分析的实际类型,但这在实际例子中很少有用,因此没有实现.
| 归档时间: |
|
| 查看次数: |
1762 次 |
| 最近记录: |