我尝试创建一个重载函数,返回void或字符串,如下所示:
public string Message { get; private set; }
public void Foo (Bar bar)
{
Message = "Hello World!";
}
public string Foo (Bar bar)
{
return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)
但我得Compile Time错误说:
Type 'Foo' already defines a member called with the same parameter types.
Run Code Online (Sandbox Code Playgroud)
有什么方法可以实现那种过载吗?
不,没有办法超载.
编译器在此行上调用什么方法:
Foo (new Bar());
Run Code Online (Sandbox Code Playgroud)
这对于两者都是完全有效的语法,无法区分,也不会发生.
方法重载必须具有不同的参数.只是不同的回报类型是不够的.
| 归档时间: |
|
| 查看次数: |
216 次 |
| 最近记录: |