base() 的用法有意义吗?

Is the usage of base() meaningful?

在阅读我公司的代码时, 我发现下面有很多代码

class Child:Parent
{
    public Child():base()
    {
        //do something
    }
}

我的问题是:base()的用法是否有意义? 在调用Child()之前,会自动调用base(),对吧? 我的意思是没有参数的构造函数。

是的,你是对的! :base() 在这种情况下不是必需的,但如果您的父 class 没有无参数构造函数,则它是必需的。

遵循 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.

希望对您有所帮助!