Constructors,Static Constructors and Destructors Execution in Inheritance in C#

No.of Views1086
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  10 May 2010 09:05:34
Tag : CSharp , General
While taking interview for .NET Technologies i often ask about the execution sequence of the constructor and destructor in inheritance But from the my experience i have found that lots of people are still confused with execution sequence of constructor and destructors
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

While taking interview for .NET Technologies i often ask about the execution sequence of the constructor and destructor in inheritance But from the my experience i have found that lots of people are still confused with execution sequence of constructor and destructors. Lets create a simple example and learn some basic things that is very important while using inheritance in C#.

  • Constructors will be executed in from parent to child sequence means first parent class constructor will be executed then after that child class constructor will be executed.
  • Destructors execution order is reverse then constructors first it will execute child class destructor and then it will execute the parent class destructor.
  • Static constructors are different then the normal constructors and its executes when first object of class is created it will be executed. Most of people are very confused this kind of scenario in inheritance. Here scenario will be like when the first object of child class created then it will execute the child class static constructor and then after the parent class static constructor is executed. After that it will never got executed.

 

Lets create a simple class which will illustrate the above worlds. First lets create a class A with constructor,destructor and a static constructor.

public class A
 {
     static A()
     {
         System.Console.WriteLine("A Static Constructor");
     }
     public A()
     {
         System.Console.WriteLine("A public constructor");
     }

     ~A()
     {
         System.Console.WriteLine("A Destructor");
     }
 }

Now we will inherit this class with the another class B which is also having constructor,destructor and a static constructor.

public class B : A
 {
     static B()
     {
         System.Console.WriteLine("B Static Constructor");
     }
     public B()
     {
         System.Console.WriteLine("B public constructor");
     }

     ~B()
     {
         System.Console.WriteLine("B Destructor");
     }
 }

Now lets create two objects of Class B in main function of our console application to see how constructors works and after that we will destroy the object via assigning null values and then forcefully we will do Garbage Collection via GC.Collect() to see how destructors works below is the code for that.

class Program
 {
     static void Main(string[] args)
     {
         B b1 = new B();
         B b2 = new B();
         //code return to destroy object of a class
         b1=b2 = null;
         GC.Collect();
         Console.ReadLine();

     }
 }

After running the console application output will be as follows. 

Image Loading

As you can see static constructors are only executed when the first object of a class is created and Constructors are executed like parent to child way and in reverse destructors are executed from child to parent way. Hope this will help you understand execution sequence of constructor in inheritance.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    Are you somewhat confused between Serialization and Marshaling? This writing would break this confusion up, it would give you a basic understanding of the process of Serialization and the process of Marshaling, and how you can get the most out of each.
    Published Date : 10/May/2010
    First, this writing concentrates of and compares between three programming languages, C#, C++/CLI, and ISO/ANSI C++. It discusses 9 rules that every developer should keep in mind while working with constructors, destructors, and finalizers and class hierarchies:
    Published Date : 05/May/2010
    This article explains how to filter rows in a DataSet/DataTable. The example provided will help you get information faster.
    Published Date : 23/May/2010
    In this article, I will explain three basic terms of C#, such as Call Stack ,Call Site and stack Unwinding
    Published Date : 17/Aug/2010
    In this article, I will discuss about Checked and unchecked keyword and conversions in C#.
    Published Date : 16/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top