Overriding constructors

  • Thread starter Thread starter Samer
  • Start date Start date
S

Samer

I'm writing a class that derives from ServiceBase and it says in the
documentation for the constructor of ServiceBase that if you override
the base class constructor, you should explicitly call it in the
constructor of your derived class.

As far as I know a constructor cannot be overridden only overloaded,
is this a mistake or have I been mislead.
 
You should be able to write a constructor for your class and explicitly call
the base implementation, effectively overriding it. You can also overload
the constructor, if you need params passed in when you create the object. I
have not tried with ServiceBase, in particular, but your new derived class
can have a default constructor as well as any number of parameterized
constructors.

I do not necessarily consider this to be overriding so much, so the
terminology is suspect.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
Samer said:
I'm writing a class that derives from ServiceBase and it says in the
documentation for the constructor of ServiceBase that if you override
the base class constructor, you should explicitly call it in the
constructor of your derived class.

As far as I know a constructor cannot be overridden only overloaded,
is this a mistake or have I been mislead.

You're correct - constructors can't be overridden, as they're not
inherited. However, I believe it's basically saying that unless you
*want* to call the parameterless constructor of the base type (in which
case, making that fact explicit isn't a bad idea if your constructor
takes any arguments) you have to call a different constructor of the
base type.

See http://www.pobox.com/~skeet/csharp/constructors.html for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top