[英]Do I call the base constructor when deriving from ServiceBase?
Microsoft .NET documentation for the System.ServiceProcess.ServiceBase class constructor says: System.ServiceProcess.ServiceBase类构造函数的Microsoft .NET文档说:
If you override the base class constructor, you should explicitly call it in the constructor of your derived class. 如果重写基类构造函数,则应在派生类的构造函数中显式调用它。
In Using Constructors in the Microsoft C# Programming Guide, it says: 在“Microsoft C#编程指南”中的“ 使用构造函数 ”中,它说:
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. 在派生类中,如果未使用base关键字显式调用基类构造函数,则会隐式调用默认构造函数(如果有)。
So do I need to call the base constructor explicitly or not, and why? 所以我需要明确地调用基本构造函数,为什么?
It doesn't matter. 没关系。 These compile to the same IL: 这些编译成相同的IL:
public class Service1 : ServiceBase
{
public Service1() : base() { }
}
public class Service1 : ServiceBase
{
public Service1() { }
}
I don't know why ServiceBase
recommends explicitly calling it. 我不知道为什么ServiceBase
建议显式调用它。 I'd go with the other suggestion, since it seems to make more sense. 我会选择其他建议,因为它似乎更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.