Will the base class constructor be automatically called?
This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of Most Base -> Most Derived.
So in your particular instance, it calls Person()
, and then Customer()
in the constructor orders. The reason why you need to sometimes use the base
constructor is when the constructors below the current type need additional parameters. For example:
public class Base
{
public int SomeNumber { get; set; }
public Base(int someNumber)
{
SomeNumber = someNumber;
}
}
public class AlwaysThreeDerived : Base
{
public AlwaysThreeDerived()
: base(3)
{
}
}
In order to construct an AlwaysThreeDerived
object, it has a parameterless constructor. However, the Base
type does not. So in order to create a parametersless constructor, you need to provide an argument to the base constuctor, which you can do with the base
implementation.
What are the rules for calling the base class constructor?
Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. Unlike Java, C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".
class SuperClass
{
public:
SuperClass(int foo)
{
// do something with foo
}
};
class SubClass : public SuperClass
{
public:
SubClass(int foo, int bar)
: SuperClass(foo) // Call the superclass constructor in the subclass' initialization list.
{
// do something with bar
}
};
More info on the constructor's initialization list here and here.
Does a base class's constructor and destructor get called with the derived ones?
It should say
class Banana : public MyBase
{
public:
Banana(void);
~Banana(void);
};
The constructor of the derived class gets called after the constructor of the base class. The destructors get called in reversed order.
How to avoid calling the base class constructor
A constructor of the base class is always used - but when the base class has no explicitly defined constructor, C# will generate a public parameterless constructor automatically.
Similarly, if you don't explicitly mention : base(...)
in your derived class, C# assumes you want to use the parameterless constructor of the base class.
As soon as you define a constructor, the parameterless constructor is not automatically generated anymore, and so you have to call : base(...)
.
You can easily change this by adding such a constructor to the base class:
public class ExportData
{
protected ExportData()
{
}
}
By making this constructor protected, only classes that inherit from ExportData
can use this constructor, and they no longer have to include : base(...)
.
Base constructor in C# - Which gets called first?
The base constructor will be called first.
try it:
public class MyBase
{
public MyBase()
{
Console.WriteLine("MyBase");
}
}
public class MyDerived : MyBase
{
public MyDerived():base()
{
Console.WriteLine("MyDerived");
}
}
C++ calling base class constructors
The short answer for this is, "because that's what the C++ standard specifies".
Note that you can always specify a constructor that's different from the default, like so:
class Shape {
Shape() {...} //default constructor
Shape(int h, int w) {....} //some custom constructor
};
class Rectangle : public Shape {
Rectangle(int h, int w) : Shape(h, w) {...} //you can specify which base class constructor to call
}
The default constructor of the base class is called only if you don't specify which one to call.
Related Topics
Is Unityscript/JavaScript Discontinued
Wix Silent Install Unable to Launch Built in .Exe: Wix V3
How to Implement a Rule Engine
Deciding on When to Use Xmldocument VS Xmlreader
JSON.Net Self Referencing Loop Detected
How Can Xml Documentation for Web API Include Documentation from Beyond the Main Project
Could Not Load File or Assembly 'System.Web.Http 4.0.0 After Update from 2012 to 2013
Should I Take Ilogger, Ilogger<T>, Iloggerfactory or Iloggerprovider for a Library
Event Signature in .Net -- Using a Strong Typed 'Sender'
How to Access Property of Anonymous Type in C#
Event Action<> VS Event Eventhandler<>
Custom Authentication in ASP.NET-Core
C# Static Member "Inheritance" - Why Does This Exist at All
Start May Not Be Called on a Promise-Style Task. Exception Is Coming
Async and Await - Difference Between Console, Windows Forms and ASP.NET