【发布时间】:2011-11-06 00:43:02
【问题描述】:
我的代码是
public class Parent
{
public Parent(int i)
{
Console.WriteLine("parent");
}
}
public class Child : Parent
{
public Child(int i)
{
Console.WriteLine("child");
}
}
我收到错误:
Parent 不包含接受 0 个参数的构造函数。
我知道问题是Parent 没有带有 0 个参数的构造函数。但我的问题是,为什么我们需要一个零参数的构造函数?为什么没有它代码就不能工作?
【问题讨论】:
-
在使用来自另一个程序集的内部构造函数扩展抽象类时,由于另一个原因,我收到了相同的错误消息。目前 DotNetCore 中的 TypeInfo、FieldInfo、...就是这种情况。只是想分享这些知识,因为我花了几个小时才解决它。
标签: c# inheritance constructor compiler-errors