且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#错误:家长不包含一个构造函数的参数0

更新时间:2022-12-08 19:09:42

既然你不明确地调用父类的构造为您的孩子类的构造函数的一部分,还有就是插入一个无参数父类的构造隐式调用。该构造函数不存在,所以你会得到错误。

Since you don't explicitly invoke a parent constructor as part of your child class constructor, there is an implicit call to a parameterless parent constructor inserted. That constructor does not exist, and so you get that error.

要纠正这种情况,你需要添加的显式调用:

To correct the situation, you need to add an explicit call:

public Child(int i) : base(i)
{
    Console.WriteLine("child");
}

或者,你可以添加一个无参数的父类的构造:

Or, you can just add a parameterless parent constructor:

protected Parent() { }