.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Simple invoking order of Constructors

Posted By:Jean Paul       Posted Date: December 16, 2010    Points: 75    Category: .Net Framework    URL: http://www.dotnetspark.com  

Showing the order in which constructors are called when inherited classes exists

In this article I am trying to show the order in which constructor and destructors are called for a class and its base class.


It is of great importance that we should know constructors work in an inherited manner - especially in interviews.

This will give us more clarity in creating or destroying objects in constructors/destructors.


We have a class named ChildClass having a constructor and destructor

Also, a class named ParentClass having a constructor and destructor


Whenever the constructor or destructor is called it logs a message to the main form.


We create an instance of ChildClass and make Garbage Collector to destroy the instance.

Thus we are able to invoke the entire constructors/destructors involved.

new ChildClass();


We are able to see the result like: Even though we are creating the instance of ChildClass the ParentClass constructor is invoked first.

But while on destruction the ChildClass destructor is invoked first.


Class: ParentClass

public class ParentClass


    public ParentClass()


        MainForm.AddMessage("ParentClass constructor invoked");





        MainForm.AddMessage("ParentClass destructor invoked");



Class: ChildClass


public class ChildClass : ParentClass


    public ChildClass()


        MainForm.AddMessage("ChildClass constructor invoked");





        MainForm.AddMessage("ChildClass destructor invoked");



 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend