我什么时候需要在C#中调用base()?

我什么时候需要在C#中调用base()?,c#,inheritance,constructor,C#,Inheritance,Constructor,我的基类构造函数被调用,而我在派生类中有一个构造函数,所以我什么时候需要调用base() 输出 'Inheritance.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll' 'Inheritance.vshost.exe' (Managed (v4.

我的基类构造函数被调用,而我在派生类中有一个构造函数,所以我什么时候需要调用base()

输出

'Inheritance.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'
'Inheritance.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
BaseClass
InheritedClass
The thread 'vshost.RunParkingWindow' (0x12b4) has exited with code 0 (0x0).
The thread '<No Name>' (0x85c) has exited with code 0 (0x0).
The program '[4368] Inheritance.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[4368] Inheritance.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
'heritation.vshost.exe'(托管的(v4.0.30319)):加载的'C:\WINDOWS\Microsoft.Net\assembly\GAC\U MSIL\Accessibility\v4.0\U 4.0.0.0\Uuu b03f5f7f11d50a3a\Accessibility.dll'
“heritation.vshost.exe”(托管(v4.0.30319)):加载的“C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
基类
继承类
线程“vshost.RunParkingWindow”(0x12b4)已退出,代码为0(0x0)。
线程“”(0x85c)已退出,代码为0(0x0)。
程序“[4368]heritation.vshost.exe:程序跟踪”已退出,代码为0(0x0)。
程序“[4368]heritation.vshost.exe:Managed(v4.0.30319)”已退出,代码为0(0x0)。

当您有一个非默认的基本构造函数(例如带有参数重载)时

例如:

public class BaseClass
{
  public BaseClass(int number)
  {
     // DoSomething
  }

  public BaseClass(string str)
  {
    // Do Something
  }
}

public class DerivedClass : BaseClass
{
   public DerivedClass(): base(7){} 
   public DerivedClass(string str) : base(str){}
}
默认的
base()
构造函数本身被自动调用

public class DerivedClass : BaseClass
{
   public DerivedClass(): base() // No harm done, but this is redundant
   {}
   //same as
   public DerivedClass()
   {}
}

它唯一没有被自动调用的时间是调用其他东西时,比如参数化的基本构造函数。正如阿米尔·波波维奇(Amir Popovich)出色的回答一样。

谢谢,这比我读不懂的另一条线索的数百行更清楚。不,这不是重复的。另一个问题是关于如何在精确的上下文中进行细化。我的问题是关于哪些上下文。
public class DerivedClass : BaseClass
{
   public DerivedClass(): base() // No harm done, but this is redundant
   {}
   //same as
   public DerivedClass()
   {}
}