C# Error CS0121 – The call is ambiguous between the following methods or properties: ‘method1’ and ‘method2’

C# Compiler Error

CS0121 – The call is ambiguous between the following methods or properties: ‘method1’ and ‘method2′

Reason for the Error

You will receive this error when you usually call one of the overloaded method and the C# compiler is not able to call it because of the conflict with the parameters caused by implicit conversion.

For example, let’s have a look at the below C# code snippet.

namespace DeveloperPublishNamespace
{
    public class DeveloperPublish
    {
        public static void Method1(int input1,double input2)
        { 

        }
        public static void Method1(double input1,int input2)
        {

        }
        
        public static void Main()
        {
            Method1(5,5);        
		}
    }
}

This contains 2 overloaded functions with different parameter type. When this function is called with the parameter 5,5, it results with the below error.

Error CS0121 The call is ambiguous between the following methods or properties: ‘DeveloperPublish.Method1(int, double)’ and ‘DeveloperPublish.Method1(double, int)’ ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 16 Active

C# compiler was not able to identify which overloaded method to call because of the values that is passed which results in the implicit conversion.

Solution

There are plenty of ways in which you can avoid this error. You can either specify the parameter type with the explicit conversion so that the implicit conversion doesn’t take place or possibly use a named arguments.

    1 Comment

  1. Alexander
    November 24, 2021
    Reply

    For more complicated case (when both signatures are exactly the samem but imported from different dlls) see here:
    https://stackoverflow.com/questions/31729186/c-sharp-compiler-cs0121-the-call-is-ambiguous-between-the-following-methods-or

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...