c# - 使用base()有意义吗?

标签 c# parameters constructor radix

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

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/

相关文章:

C# Winform 按名称动态加载用户控件

c# - 正则表达式匹配仅由一个空格分隔的 double

java - 选择查询中的 JDBCPreparedStatement 和参数 (?)

Jquery从MVC url获取参数Id?

c# - 为什么 LINQ 不缓存枚举?

c# - 当 MSTest 在 TestInitialize 中失败时,为什么不执行 TestCleanup?

bash - bash 中 "$*"有哪些用例?

constructor - Findbugs 在验证构造函数参数时报告已知空值的负载

java - Java中的构造函数返回什么?

java - 在Java中模拟构造函数重写?