简体   繁体   中英

Do I call the base constructor when deriving from ServiceBase?

Microsoft .NET documentation for the System.ServiceProcess.ServiceBase class constructor says:

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:

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.

So do I need to call the base constructor explicitly or not, and why?

It doesn't matter. These compile to the same IL:

public class Service1 : ServiceBase
{
    public Service1() : base() { }
}
public class Service1 : ServiceBase
{
    public Service1() { }
}

I don't know why ServiceBase recommends explicitly calling it. I'd go with the other suggestion, since it seems to make more sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM