Boric
Boric

Reputation: 872

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?

Upvotes: 6

Views: 447

Answers (1)

Tim S.
Tim S.

Reputation: 56546

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.

Upvotes: 5

Related Questions