【问题标题】:Ambiguous call functions in the class类中不明确的调用函数
【发布时间】:2013-04-02 09:24:09
【问题描述】:
如何出行?我不想拥有不同名称的函数。
public class DataRowSafe
{
public String Get(String Column)
{
return String.Empty;
}
public int Get(String Column)
{
return 0;
}
}
DataRowSafe r=new DataRowSafe();
String res=r.Get("Column1");
int res2=r.Get("Column2");//<--- Ambiguous call
【问题讨论】:
标签:
c#
class
function
call
ambiguous
【解决方案1】:
方法的重载要求您的类似名称的方法具有不同的签名。返回值是微不足道的!在这里查看this 教程。
【解决方案3】:
你可以像这样引用参数
public class DataRowSafe
{
public void Get(String Column, ref string myParam)
{
myParam = String.Empty;
}
public void Get(String Column,ref int myParam)
{
myParam = 0;
}
}
int i = 0;
string st = "";
new DataRowSafe().Get("name", ref i);
new DataRowSafe().Get("name", ref st);
【解决方案4】:
你应该得到一个像
这样的错误
'DataRowSafe' 已经定义了一个名为 'Get' 的成员
参数类型
函数的返回类型并不重要,但在这种情况下,编译器会混淆可用于调用的两种方法,并且不确定要选择哪个方法,也许您可以使用泛型来克服这个问题
样例
public static T GetValue<T>(string column)
{
string returnvalue="";
//process the data ...
return (T)Convert.ChangeType(returnvalue, typeof(T), CultureInfo.InvariantCulture);
}
【解决方案5】:
这是不可能的,因为重载仅适用于不同的签名。如果签名相同,则 c# 编译器将返回错误。