C# 以下方法或属性之间的调用不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8845494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:46:24  来源:igfitidea点击:

The call is ambiguous between the following methods or properties

c#.netoverloading

提问by Agon Eous

Suppose I have these two ctors:

假设我有这两个演员:

public SomeClass(string a, Color? c = null, Font d = null)
        {
            // ...
        }

public SomeClass(string a, Font c = null, Color? d = null)
        {
            // ...
        }

~and I do this:

~我这样做:

SomeClass sc = new SomeClass("Lorem ipsum");

I'll get this: "Error 1 The call is ambiguous between the following methods or properties [...]"

我会得到这个:“错误 1 ​​调用在以下方法或属性之间不明确 [...]”

It seems apparent to me that it doesn't matter which one I refer to as the end result is the same (at least in this particular case, and to me that's all that matters right now), so what are my options to getting around this?

对我来说很明显,我指的是哪一个并不重要,因为最终结果是相同的(至少在这种特殊情况下,对我来说这就是现在最重要的),那么我有什么选择来解决这个?

EDIT 1: @oltman: Simplified example.

编辑 1:@oltman:简化示例。

I just want to be able to write

我只想能够写

[...] new SomeClass("Lorem", Color.Green)

instead of

代替

[...] new SomeClass("Lorem", null, Color.Green)

采纳答案by Phil Klein

Both constructors take the same number of arguments, but in a different order. Since you have specified default values for the two constructor parameters the compiler cannot distinguish between the two overloads when the second argument is not supplied.

两个构造函数都采用相同数量的参数,但顺序不同。由于您已为两个构造函数参数指定了默认值,因此当未提供第二个参数时,编译器无法区分这两个重载。

I would advise you to remove the existing constructors and replace with the following:

我建议您删除现有的构造函数并替换为以下内容:

public SomeClass(string a, Color? color, Font font)
{
    // constructor implementation
}

public SomeClass(string a) : this(a, null, null) {}
public SomeClass(string a, Color color) : this(a, color, null) {}
public SomeClass(string a, Font font) : this(a, null, font) {}

回答by RQDQ

One way to force it to work:

强制它工作的一种方法:

SomeClass sc = new SomeClass("Lorem ipsum", (Color?)null, (Font)null);

回答by nnnnnn

Can you create another constructor that takes just the string, and update the above constructors to make their second parameters mandatory?

您能否创建另一个仅接受字符串的构造函数,并更新上述构造函数以使其第二个参数成为必需?

If the idea is that you can construct the object by always supplying the string and then optionally supplying color or font or both, how about this:

如果您的想法是您可以通过始终提供字符串然后可选地提供颜色或字体或两者来构造对象,那么如何:

public SomeClass(string a)
        {
            // ...
        }

public SomeClass(string a, Color? c)
        {
            // ...
        }

public SomeClass(string a, Font f, Color? d = null)
        {
            // ...
        }

回答by matt

I'll get this: "Error 1 The call is ambiguous between the following methods or properties [...]"

It seems apparent to me that it doesn't matter which one I refer to as the end result is the same

我会得到这个:“错误 1 ​​调用在以下方法或属性之间不明确 [...]”

对我来说很明显,我指的是哪一个并不重要,因为最终结果是相同的

The call isambiguous. Each constructor is unique - it doesn't matter if they both create and return an instance, because there could be different logic in each constructor. The compiler still doesn't know which constructor you mean.

调用模棱两可的。每个构造函数都是独一无二的——它们是否都创建并返回一个实例并不重要,因为每个构造函数中可能有不同的逻辑。编译器仍然不知道您指的是哪个构造函数。

回答by Shahar Shokrani

This is a perfect example of anti-pattern, the best way avoiding this is @Phil Klein's answer.

这是一个完美的例子anti-pattern,避免这种情况的最好方法是@Phil Klein 的回答。

Here is another syntax for passing class as null:

这是将类传递为 null 的另一种语法:

SomeClass sc = new SomeClass("Lorem ipsum", null as Color, null as Font);