【问题标题】:Object doesn't take a constructor that takes 2 arguments [duplicate]对象不采用带有 2 个参数的构造函数 [重复]
【发布时间】:2018-10-02 20:43:44
【问题描述】:

我已经生成了我的问题的这个简化版本:

public class Variable
{
    public Variable(string s, int i)
    {

    }
    public Variable(string str) : base(str, 0) // error here
    {

    }
}

显然我有一个带有 2 个参数的构造函数。 但错误是说我没有。

我很困惑。

我正在使用 .NET Standard 2.0

请要求任何其他说明。

【问题讨论】:

  • : this(str, 0) 而不是 : base(str, 0)

标签: c#


【解决方案1】:

base 类(在您的情况下为object没有这样的构造函数

 object(string s, int i)

但是您的 currentthis 确实具有所需的构造函数:

public class Variable
{
    public Variable(string s, int i)
    {

    }

    public Variable(string str) : this(str, 0) // current class constructor call
    {

    }
}

【讨论】:

  • 感谢您拯救了我的一天。我只知道base() 会在 c# 中重载构造函数。它(this 的使用)仅在 .net-standard 中还是在 c# 中的任何地方?
  • @Muzib: this 作为当前类构造函数可以在c#中无处不在使用
【解决方案2】:
: base(str, 0)

正在调用 Object 构造函数,该构造函数没有 2 个参数。

改用这个

: this(str, 0)

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多