【发布时间】:2016-02-16 20:05:21
【问题描述】:
由于参数排列不同,得到模糊调用:short,int / int,short /byte,int / int,byte
因为函数签名是:
1.参数/参数个数
2.参数/参数的类型
3.自变量/参数的排列
为什么调用不明确?它应该属于相似的类型吗?...
代码:
class Program
{
static void Main(string[] args)
{
test abbb = new test();
//abbb.add(2.2f,1);
// abbb.add(2,2.2f);
abbb.add(255,1);
abbb.add(1,256);
Console.ReadLine();
}
}
class test
{
public int add(byte i , int f) {
return i + f;
}
public int add(int i, byte f)
{
return i + f;
}
public int add(short i, int f)
{
return i + f;
}
public int add(int i, short f)
{
return i + f;
}
}
【问题讨论】:
-
您能否逐步完成您认为应该是所选重载的过程?如果您也想得到答案,可以使用 C# 语言规范来做到这一点。
-
看看如果你添加一个需要两个整数的方法会发生什么。现在没有错误,因为这两个整数常量有一个完美匹配的目标。缺少编译器只能找到两个方法,作为第一个参数一个整数,但它无法决定是否应该将第二个参数转换为字节或短
标签: c# overloading ambiguous overload-resolution