在阅读我公司的代码时, 我发现下面有很多代码
class Child:Parent
{
public Child():base()
{
//do something
}
}
我的问题是使用:base()
是否有意义?
在调用 Child()
之前,会自动调用 base()
,对吗?
我的意思是不带参数的构造函数。
最佳答案
是的,你说得对!在这种情况下, :base() 不是必需的,但如果您的父类没有无参数构造函数,则 :base() 是必需的。
遵循 MSDN 文档 https://msdn.microsoft.com/en-us/library/ms173115(v=vs.90).aspx
In a derived class, if a base-class constructor is not called explicitly by using the base keyword, the default constructor, if there is one, is called implicitly. This means that the following constructor declarations are effectively the same:
public Manager(int initialdata)
{
//Add further instructions here.
}
public Manager(int initialdata)
: base()
{
//Add further instructions here.
}
If a base class does not offer a default constructor, the derived class must make an explicit call to a base constructor by using base.
希望对你有帮助!
关于c# - 使用base()有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085108/