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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...