Skip to main content
 首页 » 编程设计

c#之基础构造函数的使用

2024年02月13日18qlqwjy

我正在维护的一个 C# 应用程序出现问题。我看到一些我不明白的东西,所以我想要求澄清。它可能与我遇到的问题有关,也可能无关。

class C2iModel 
{ 
    public C2iModel() { //blah } 
} 
 
class EplrsModel : C2iModel 
{ 
    public EplrsModel() : base() { //blah } 
} 

我的理解是调用子构造函数时,会自动调用父构造函数。

我的问题是,在 EplrsModel 构造函数中显式调用基本构造函数是否有任何区别?

请您参考如下方法:

在您的示例中,调用基本构造函数是多余的。您只需要在传递参数时为构造函数显式指定它。示例:

class Base { 
 
  public Base(string type) { ... } 
} 
 
class Extend : Base { 
 
  public Extend(string type, string name) : base(type) { ... } 
 
}