Constructors Execution Sequence:
When inheritance is existing in a class hierarchy, we must understand the process of object creation. Let us observe the following code and try to debug it in visual studio and understand the sequence of code execution.
When the source code is debugged in visual studio by having the breakpoints at lines 11, 18, 25 and 32 we can observe that
1. The CLR observes only the syntax of respective inherited class of D while creating object of D. (i.e. D - C - B - A)
2. But creates the object of A class then B, C and D. (i.e. A - B - C - D)
3. Hence while creating object of Derived class, Base class comes into existence first.
* The statement in line 5 creates object of D using new keyword followed by calling the parameter-less constructor.
* At first, CLR observes the syntax of all the classes in hierarchy before creation of actual object in heap memory.
Did you know?
1. C# compiler converts the constructors into equivalent methods with return type as void and also it adds ':base()' at the end of constructor to pass the call to its immediate base class. This ':' colon symbol doesn't mean that constructor is inheriting something, it just passes the call to base class constructor.
2. If no constructor is used by the user, C# compiler creates parameter-less constructor which is known as Default Constructor
(The modifications made by C# compiler are given in red text.)
Parameter-less constructors are shown here for ease of understanding. The same sequence is applicable for any constructor independent of number of parameters. Also the local variables present in parameters of a constructor can be passed on to its base class by including the same parameter manually inside brackets of ":base()". Observe the following example to understand calling sequence when base() is manually handled to divert the call during object creation.
public partial class ConSequence : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
C c = new C("Mohan", 50000.00f, 044222222, 9987654321);
public class A : Object
long phone, mobile;
public A(long phone, long mobile)
this.phone = phone;
this.mobile = mobile;
public class B : A
public B(float salary, long phone, long mobile)
: base(phone, mobile)
this.salary = salary;
public class C : B
public C(string name, float salary, long phone, long mobile)
: base(salary, phone, mobile)
this.name = name;
All the necessary data is passed into constructor of C class and using the parameters respective values are passed onto respective base classes. After defining class A, B and C, the object of C is created under Page_Load method. In visual studio we can observe the expected parameters to create object of class C as shown in the figure.
Object of class A is created first and then Object of class B and at last class C. Since A is the most parent / base and C is the most child / derived.
C class is depending on existence of base classes B and A. If base class code is deleted, derived classes cannot exist. Whereas if derived classes are deleted, it has nothing to do with creation of object of base class. This is a logical proof for existence of base class object before derived class object.