Using your example, the answer is: YES. The base constructor will be called for you and you do not need to add one.
You are ONLY REQUIRED to use "base(...)" calls in your Derived Class
IF you added a non-default constructor with parameters to your Base Class
, but didn't add an explicit default constructor to the Base Class
, as well. That breaks the chain of calls to the Base Class
, so you need to call one of the two Base Class
constructors from your Derived Class
in that case.
You only need to make that base(...) call if you disrupt or break the implied constructor call chain to the base constructor from the derived class. That happens the minute you add a non-default constructor to your base class.
In c# using base and derived classes, some call to the base class's constructors must always be made from the derived class, either implicitly by the runtime, or explicitly by you. Most of the time the runtime will call the base class default constructor for you when you create your derived class object, so you do not need to call "base()". By default, a derived class when instantiated will always IMPLICITLY call the base class default constructor. That is why in most cases, you do NOT need to add "base()" to a derived class's constructor. A base class is always instantiated first from the derived class via a call to the default constructor in the base class, UNLESS you change its constructors (see below). C# doesn't care if it is a default constructor or non-default constructor with parameters. One must be called in the base class when the derived class object is created.
That is why you can leave out both a "base()" call in your derived class constructor and an explicit default constructor in all your classes as its implicitly called.
The implicit base class constructor call is only true and made for you if one of the following is true. When true, you do NOT need to call "base(...)" in your derived class:
- Your base class has no explicit constructors defined
- You base class has only an explicit default constructor defined
- You base class has one or more non-default constructors defined with parameters AND an explicit default constructor is defined.
When you suddenly add a non-default constructor with parameter(s) and NO default constructor, it breaks the default hidden default constructor chain creation and calls and you HAVE to add "base()". In your Base class with a non-default constructor you must now either call that constructor explicitly from the derived class using "base(...)", or add a default constructor explicitly in the base class. If the latter you can avoid "base()" calls. It is implicitly called.
Let's test this.....
// IMPLIED CONSTRUCTOR CALL TO THE BASE CLASS CALL WORKS NATURALLY HERE
class MyBaseClass0
{
// a default constructor added for you
}
class DerivedClass0 : MyBaseClass0
{
// an implied call to the base constructor is done for you
}
// THIS WORKS!!!
class MyBaseClass1
{
// a default constructor added for you
}
class DerivedClass1 : MyBaseClass1
{
public DerivedClass1()
{
// Here the derived class default constructor is
// created explicitly but an implied call to the
// base constructor is done for you
}
}
// AND THIS WORKS!!!
class MyBaseClass2
{
// a default constructor added for you
}
class DerivedClass2 : MyBaseClass2
{
public DerivedClass2() : base()
{
// "base()" is still optional here as implied call
// to the base constructor is done for you
}
}
// AND THIS WORKS!!!
class MyBaseClass3
{
// a default constructor added for you
}
class DerivedClass3 : MyBaseClass3
{
public DerivedClass3(int x)// base not needed
{
// an implied call to the base constructor is still done for you
}
}
// BECAUSE WE ADDED A NON-DEFAULT CONSTRUCTOR WITH
// NO EXPLICIT DEFAULT CONSTRUCTOR WE MUST USE "BASE(...)"
class MyBaseClass4
{
// need explicit default constructor or must use "base()" now
// non-default constructor added which breaks implicit call
public MyBaseClass4(string y)
{
}
}
class DerivedClass4 : MyBaseClass4
{
public DerivedClass4(int x) : base("hello")
{
// The implicit call to the default constructor is broken now
// because we added a non-default constructor to base class.
// Adding a "base()" call we have fulfilled the requirement
// that some base constructor be called in the Base Class.
}
}
// The IMPLIED default constructor call now works again
// because we added an explicit default constructor beside
// the non-default one we added. "base()" is not needed again.
class MyBaseClass5
{
public MyBaseClass5()
{
}
public MyBaseClass5(string y)
{
}
}
class DerivedClass5 : MyBaseClass5
{
public DerivedClass5(string x)
{
}
}