Search This Blog

Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Sunday, 6 August 2017

Error CS0121 The call is ambiguous between the following methods or properties

Compiler Error CS0121 The call is ambiguous between the following methods or properties: 'TestMethod' and 'TestMethod'

The compiler was not able to call one form of an overloaded method. In other word Compiler can not decide which overload method should use.

C# Code

public class Parent
{
    public double Add(int i, double d)
    {
        return (double)i + d;
    }

    public double Add(double d, int i)
    {
        return d + (double)i;
    }
}

Call above function from Main function

static void Main(string[] args)
{
    Parent p1 = new Parent();
    double result1 = p1.Add(1, 2.2); // result1 = 3.2
    double result2 = p1.Add(1.1, 2); // result1 = 3.1
    double result3 = p1.Add(1, 2);   // Get Compiler Error CS0121
    Console.ReadKey();
}

Why function overloading is not based on return type?

Compiler Error CS0121 The call is ambiguous between the following methods or properties: 'TestMethod' and 'TestMethod'

The compiler was not able to call one form of an overloaded method. In other word Compiler can not decide which overload method should use.

C# Code

public class Parent
{
    public void TestMethod()
    {
        Console.WriteLine("Message from Parent.TestMethod().");
    }

    public string TestMethod()
    {
        Console.WriteLine("Message from Parent.TestMethod() return type string.");
        return string.Empty;
    }
}

Observation

Here, will get 2 error for above C# sample code

Solution

You can resolve above error in 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 method.

Creating a NuGet Package Feed to Host Artifacts

Step-by-Step Guide: Creating a NuGet Package Feed to Host Artifacts 🔹 Step 1: Create a C# Class Library and Generate NuG...

Recent Post