Compiler error CS0121
The call is ambiguous between the following methods or properties: 'method1' and 'method2'
Due to implicit conversion, the compiler was not able to call one form of an overloaded method. You can resolve this error in one of the following ways:
- Specify the method parameters in such a way that implicit conversion does not take place.
- Remove all overloads for the method.
- Cast to proper type before calling the method.
- Use named arguments.
Example 1
The following examples generate compiler error CS0121:
public class Program
{
static void f(int i, double d)
{
}
static void f(double d, int i)
{
}
public static void Main()
{
f(1, 1); // CS0121
// Try the following code instead:
// f(1, 1.0);
// or
// f(1.0, 1);
// or
// f(1, (double)1); // Cast and specify which method to call.
// or
// f(i: 1, 1);
// or
// f(d: 1, 1);
// f(i: 1, d: 1); // This still gives CS0121
}
}
Example 2
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();
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for