Compiling the following code will return The call is ambiguous between the following methods or properties
error. How to resolve it since I can't explicitly convert null
to any of those classes?
static void Main(string[] args)
{
Func(null);
}
void Func(Class1 a)
{
}
void Func(Class2 b)
{
}
You could also use a variable:
The
Func()
methods accept a reference type as a parameter, which can be null. Since you're calling the method with an explicitnull
value, the compiler doesn't know whether your null is supposed to be in reference to aClass1
object or aClass2
object.You have two options:
Cast the null to either the
Class1
orClass2
type, as inFunc((Class1)null)
orFunc((Class2)null)
Provide a new overload of the
Func()
method that accepts no parameters, and call that overload when you don't have an explicit object reference:Just an alternative solution I prefer
Cast
null
to the type:You should be able to cast null to either of those, the same as you would a variable
Func((Class1)null)
.