![](/img/trans.png)
[英]When inheriting from a framework control, should I call the base constructor?
[英]Do I call the base constructor when deriving from ServiceBase?
System.ServiceProcess.ServiceBase類構造函數的Microsoft .NET文檔說:
如果重寫基類構造函數,則應在派生類的構造函數中顯式調用它。
在“Microsoft C#編程指南”中的“ 使用構造函數 ”中,它說:
在派生類中,如果未使用base關鍵字顯式調用基類構造函數,則會隱式調用默認構造函數(如果有)。
所以我需要明確地調用基本構造函數,為什么?
沒關系。 這些編譯成相同的IL:
public class Service1 : ServiceBase
{
public Service1() : base() { }
}
public class Service1 : ServiceBase
{
public Service1() { }
}
我不知道為什么ServiceBase
建議顯式調用它。 我會選擇其他建議,因為它似乎更有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.