How to Resolve Error "CS0121: The call is ambiguous between the following methods or properties" in C#
The Compiler Error CS0121 is a Method Overload Resolution error. The message reads: "The call is ambiguous between the following methods or properties: 'MethodA' and 'MethodB'".
C# allows Method Overloading, which means you can have multiple methods with the same name but different parameter types. When you call a method, the compiler tries to find the "best match." If the compiler finds two or more methods that are equally valid for the arguments you provided, and neither is "better" (more specific) than the other, it gives up and throws CS0121.
This guide explains how these conflicts arise and how to disambiguate your calls.
Understanding Overload Resolution
When you pass an argument to a method, the compiler looks for a match.
- Exact Match: Passing an
intto a method acceptingint. (Winner). - Implicit Conversion: Passing an
intto a method acceptingdouble. (Valid).
The error occurs when there are multiple implicit conversions that are equally costly. For example, converting an int to a double is considered just as "good" as converting an int to a float. If both methods exist, the compiler cannot choose.
Scenario 1: Implicit Numeric Conversions
This is the most common cause. You define overloads for different numeric types (like double and decimal), but call the method with an integer. Since integers can be implicitly converted to both types without data loss, the call is ambiguous.
Example of error
public class Calculator
{
public void Process(double d)
{
System.Console.WriteLine("Processing Double");
}
public void Process(decimal d)
{
System.Console.WriteLine("Processing Decimal");
}
}
public class Program
{
static void Main()
{
Calculator calc = new Calculator();
// ⛔️ Error CS0121: The call is ambiguous between 'Process(double)' and 'Process(decimal)'
// An integer literal '5' can be converted to double OR decimal.
// Neither conversion is preferred over the other.
calc.Process(5);
}
}
Solution: Explicit Casting or Suffixes
You must tell the compiler explicitly which type you intend to use.
public class Program
{
static void Main()
{
Calculator calc = new Calculator();
// ✅ Correct: Use a cast
calc.Process((double)5);
// ✅ Correct: Use a numeric suffix ('m' for decimal, 'd' for double)
calc.Process(5m);
}
}
Output:
Processing Double
Processing Decimal
Scenario 2: Default (Optional) Parameters
C# allows optional parameters (e.g., void Do(int a, int b = 0)). If you overload a method, and one of the overloads has optional parameters, you can create a situation where a single call matches both signatures perfectly.
Example of error
public class Printer
{
// Method A: Takes one integer
public void Print(int a) { }
// Method B: Takes two integers, but the second is optional
public void Print(int a, int b = 0) { }
}
public class Program
{
static void Main()
{
Printer p = new Printer();
// ⛔️ Error CS0121: Ambiguous call.
// Compiler: "Do you want Method A? Or Method B using the default value?"
p.Print(10);
}
}
Solution: Remove Ambiguity or Use Named Arguments
The best fix is usually to redesign the class so signatures don't overlap. However, if you cannot change the class, use Named Arguments (though even this can sometimes remain ambiguous depending on the structure).
Option A: Redesign (Recommended) Remove the optional parameter or rename one of the methods.
Option B: Explicit Call (If valid) Call the method with all parameters to force the second overload.
// ✅ Correct: Passing the second argument forces Method B
p.Print(10, 5);
Optional parameters combined with method overloading is a known "anti-pattern" in C# specifically because it leads to CS0121. Prefer strictly defined overloads over defaults when possible.
Scenario 3: The 'null' Ambiguity
Reference types (classes, strings, interfaces) can all be null. If you have overloads that accept different reference types, passing null directly is ambiguous because null fits into all of them.
Example of error
public class Handler
{
public void Log(string message) { }
public void Log(Exception ex) { }
}
public class Program
{
static void Main()
{
Handler h = new Handler();
// ⛔️ Error CS0121: Ambiguous call.
// 'null' is a valid string. 'null' is also a valid Exception.
h.Log(null);
}
}
Solution: Cast the Null
Cast null to the specific type you want the method to receive.
public class Program
{
static void Main()
{
Handler h = new Handler();
// ✅ Correct: Explicitly pass a null string
h.Log((string)null);
}
}
Conclusion
CS0121 means your code is too vague. The compiler refuses to guess your intention.
- Use Suffixes: When passing numbers, use
f,d,m,Lsuffixes to specify the exact type. - Use Casting: Cast arguments
(int)xor(string)nullto select a specific overload. - Avoid Default Conflicts: Be careful when mixing Method Overloading with Optional Parameters. They often conflict.