Search This Blog

Sunday, 6 August 2017

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.

No comments:

Post a Comment

Elasticsearch - Nodes, clusters, and shards

Elastic Stack Video - Load your gun in short time.   Beginner's Crash Course to Ela...

Recent Post