以下方法或属性之间的调用不明确 [英] The call is ambiguous between the following methods or properties

查看:80
本文介绍了以下方法或属性之间的调用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个ctor:

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)
        {
            // ...
        }

〜而我这样做:

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

我将得到这个消息:错误1以下方法或属性[...]之间的调用不明确"

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 (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?

@oltman:简化示例.

EDIT 1: @oltman: Simplified example.

我只是想写东西

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

代替

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

推荐答案

两个构造函数都使用相同数量的参数,但是顺序不同.由于您已经为两个构造函数参数指定了默认值,因此当不提供第二个参数时,编译器将无法在两个重载之间进行区分.

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) {}

这篇关于以下方法或属性之间的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆