更新:2007 年 11 月
错误消息
在以下方法或属性之间的调用不明确:“method1”和“method2”
因隐式转换的缘故,编译器无法调用重载方法的某种形式。可以用以下方法纠正该错误:
以不发生隐式转换的方式指定此方法的参数。
移除此方法的所有重载。
在调用方法之前,强制转换到正确的类型。
示例
下面的示例生成 CS0121:
// CS0121.cs
public class C
{
void f(int i, double d)
{
}
void f(double d, int i)
{
}
public static void Main()
{
C c = new C();
c.f(1, 1); // CS0121
// try the following line instead
// c.f(1, 1.0);
// or
// c.f(1.0, 1);
// or
// c.f(1, (double)1); // cast and specify which method to call
}
}
下面的示例在 Microsoft Visual Studio 2008 中而不是在 Visual Studio 2005 中生成 CS0121:
//CS0121_2.cs
class Program2
{
static int ol_invoked = 0;
delegate int D1(int x);
delegate T D1<T>(T x);
delegate T D1<T, U>(U u);
static void F(D1 d1) { ol_invoked = 1; }
static void F<T>(D1<T> d1t) { ol_invoked = 2; }
static void F<T, U>(D1<T, U> d1t) { ol_invoked = 3; }
static int Test001()
{
F(delegate(int x) { return 1; }); //CS0121
if (ol_invoked == 1)
return 0;
else
return 1;
}
static int Main()
{
return Test001();
}
}