代码之家  ›  专栏  ›  技术社区  ›  Muzib hardyVeles

对象不采用接受2个参数的构造函数[重复]

c#
  •  0
  • Muzib hardyVeles  · 技术社区  · 7 年前

    这个问题已经有了答案:

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

    public class Variable
    {
        public Variable(string s, int i)
        {
    
        }
        public Variable(string str) : base(str, 0) // error here
        {
    
        }
    }
    

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

    我很困惑。

    我正在使用 .NET标准2.0

    请要求进一步澄清。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Dmitrii Bychenko    7 年前

    base 类( object 在你的情况下) 没有 这样的建设者

     object(string s, int i)
    

    但是你 现在的 this 是否具有所需的构造函数:

    public class Variable
    {
        public Variable(string s, int i)
        {
    
        }
    
        public Variable(string str) : this(str, 0) // current class constructor call
        {
    
        }
    }
    
        2
  •  3
  •   Matti Price    7 年前
    : base(str, 0)
    

    正在呼叫 Object 没有一个for 2参数的构造函数。

    用这个代替

    : this(str, 0)