简体   繁体   English

如何解决此模糊调用错误

[英]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());  
}

The compiler is confused, because null matches both overloads. 编译器很困惑,因为null匹配两个重载。 You can explicitly cast null to the class that you need to let the compiler know which of the two overloads you are calling: 您可以显式地将null转换为需要让编译器知道您调用的两个重载中的哪一个的类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM