Eminem
Eminem

Reputation: 7484

How do I resolve this ambiguous call error

I get the following error at compile time. How do I resolve it without having to resort to different function names

private double SomeMethodName(SomeClassType value)
{           
    return 0.0;
}
private double SomeMethodName(ADifferentClassType value)
{
    if (value == null)
    {
        return this.SomeMethodName(null);  //<- error
    }
    return this.SomeMethodName(new SomeClassType());  
}

Upvotes: 12

Views: 4070

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

The compiler is confused, because null matches both overloads. You can explicitly cast null to the class that you need to let the compiler know which of the two overloads you are calling:

if (value == null)
{
    return this.SomeMethodName((SomeClassType)null);
}

Upvotes: 18

Related Questions