Working with normal and static constructor in C# 2.0
 
Published: 06 Oct 2006
Abstract
This Code snippet explains when a normal constructor and static constructor gets initialized. After going through this code snippet, reader gets a clear cut understanding of the steps involved in object initialization (Both static and non-static)
by Uday Denduluri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 31684/ 25

Introduction

In C#, constructor is defined as a method which gets called whenever an object is created. A constructor may or may not have arguments without any return type. In C#, we also have static constructors. Static constructors are used to initialize Static variables. Let us understand how and when these constructors get called.

Detailed Analysis

The code below explains when an instance constructor gets called in class.

Listing 1

using System;
public class A
{
  //Constructor of Class A
  public A()
  {
    Console.WriteLine("Constructor of Class A");
  }
}
 
public class B: A
{
  //Constructor of Class B
  public B()
  {
    Console.WriteLine("Constructor of Class B");
  }
}
 
public class C: B
{
  //Constructor of Class C
  public C()
  {
    Console.WriteLine("Constructor of Class C");
  }
}
 
public class Client
{
  static void Main(string[]args)
  {
    // Initializing the class C's constructor.
    C c = new C();
    Console.Read();
  }
}

Class A is the base class which has a child class B. Class C gets inherited from Class B. All these classes have public instance constructors. We create an object of Class C. Let us see the output once we create an object of Class C.

Figure 1

As shown in Figure 1, constructor of Class A gets called first, constructor of Class B gets called and constructor of Class C then. Class A is the base class and hence we understand that constructor of A gets called first. This behavior of the code is expected. Now let us see a code snippet where static constructors are also involved.

The code below exposes the difference between the instance and normal constructors getting called.

Listing 2

using System;
public class A
{
  //Static Constructor of Class A
  static A()
  {
    Console.WriteLine("Static Constructor of Class A");
  }
 
  //Constructor of Class A
  public A()
  {
    Console.WriteLine("Constructor of Class A");
  }
}
 
public class B: A
{
  //Static Constructor of Class B
  static B()
  {
    Console.WriteLine("Static Constructor of Class B");
  }
 
  //Constructor of Class B
  public B()
  {
    Console.WriteLine("Constructor of Class B");
  }
}
 
public class C: B
{
  //Static Constructor of Class A
  static C()
  {
    Console.WriteLine("Static Constructor of Class C");
  }
 
  //Constructor of Class C
  public C()
  {
    Console.WriteLine("Constructor of Class C");
  }
}
 
public class Client
{
  static void Main(string[]args)
  {
    // Initializing the class C's constructor.
    C c = new C();
    Console.Read();
  }
}

Let us have a look at the output of the code snippet.

Figure 2

Surprised to see the output! I guess this is not the expected behavior of the code snippet. Before understanding the difference between the static and instance constructors we need to understand when does static constructor gets called and when does instance constructor gets called. A static constructor gets instantiated whenever the class is loaded for the first time. Let us understand in detail what has happened.

When the .NET runtime executes the statement "new C()" it understands that it has to create an object of C. Then it calls the static constructor of class C. It then calls the static constructor of the class B and then A. The instance constructor of the class A, B and C are then called.

Conclusion

Whenever an object gets initialized .NET runtime executes the object in 2 passes. In the first pass it initializes all the static variables in the upward hierarchy by calling the static constructors. In the second pass it executes all the instance constructors in the downward hierarchy.



User Comments

Title: instance constructor & static constructor   
Name: suganya
Date: 2012-07-10 7:45:33 AM
Comment:
nice...easy to understand
Title: constructor Vs static Costructor   
Name: Latha
Date: 2012-07-10 7:44:52 AM
Comment:
Very Good & Clear Explanation i understood very well
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:30:02 PM
Comment:
[/pre]Cheap NFL,NBA,MLB,NHL
[url=http://www.jersey2shop.com/]Jerseys From China[/url]
[url=http://www.jersey2shop.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jersey2shop.com/]cheap China Jerseys[/url]
[url=http://www.jersey2shop.com/]Sports Jerseys China[/url]
[url=http://www.jersey2shop.com/NFL-Jerseys-c68/]NFL Jerseys China[/url]
[url=http://www.jersey2shop.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
NHL Jerseys China
[url=http://www.jersey2shop.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[/pre]
[pre]We Are Professional China jerseys Wholesaler
[url=http://www.cheapjersey2store.com/]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url= http://www.cheapjersey2store.com/]2012 mlb all atar jerseys[/url]
[url= http://www.cheapjersey2store.com/ [/url]Cheap China Wholesael[/url]
[url= http://www.cheapjersey2store.com/]Wholesale jerseys From China[/url]
[url=http://www.cheapjersey2store.com/]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.cheapjersey2store.com/]Jerseys From China[/url]
[url=http://www.cheapjersey2store.com/NFL-Jerseys-c68]NFL jerseys China[/url]
[url=http://www.cheapjersey2store.com/NHL-Jerseys-c96/]NHL Jerseys China[/url]
[url=http://www.cheapjersey2store.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
[url=http://www.cheapjersey2store.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]
[url= http://www.cheapjersey2store.com/]China Jerseys[/url],Free Shipping
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.jerseycaptain.com/]cheap jerseys sale online [/url]
[url= http://www.jerseycaptain.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jerseycaptain.com/NFL-Jerseys-c68]cheap NFL jerseys China[/url]
[url=http://www.jerseycaptain.com/NHL-Jerseys-c96/]NHL Jerseys C
Title: Good One   
Name: Senthilkumar G
Date: 2010-09-09 5:01:10 AM
Comment:
Its really a very good example.
Title: Nicely explained   
Name: Manu Bhatnagar
Date: 2010-02-09 5:42:09 AM
Comment:
It is very good article.
Title: Simple and Clear   
Name: Shine T Jose
Date: 2008-10-24 1:50:28 AM
Comment:
Hi,
This is a wonderful example that you have given. Its simple and very clear...
Thanks
Shine
Title: Very good   
Name: Tallam
Date: 2008-08-04 5:36:13 PM
Comment:
very good example.if you give some more details it will help full to all...
Title: Amaizing   
Name: Rejith
Date: 2008-07-28 2:12:26 AM
Comment:
simple and easy to understand.
Title: Sweet   
Name: Maneesh
Date: 2008-07-09 2:50:23 AM
Comment:
Short and Sweet Article about static constructor...
Title: Fantastic....   
Name: Hariprasad seelam
Date: 2008-07-01 7:56:51 AM
Comment:
Heart full explanation.thanks
Title: .   
Name: .
Date: 2008-06-13 7:33:18 AM
Comment:
Good job mate
Title: thx   
Name: asha
Date: 2008-06-04 7:32:43 AM
Comment:
it is a very useful info thx
Title: thanx alot   
Name: kouky
Date: 2008-05-30 2:45:00 PM
Comment:
thanx for this article
Title: very good   
Name: gadhvi
Date: 2008-05-05 9:23:16 AM
Comment:
it's really very helpfull
Title: Working with normal and static constructor in C# 2.0   
Name: Ilavarasi
Date: 2008-02-29 11:19:34 PM
Comment:
Very Good Article.Great job
Title: Good Explanation   
Name: Sri
Date: 2008-01-19 11:57:20 AM
Comment:
Very Good explanation.Hope every concept need to be explained in such a manner, simple and easy to understand.

Thanks
Srikanth
Title: Very simple   
Name: vengadesh rampro
Date: 2007-12-05 12:48:13 AM
Comment:
its simple to understand
Title: Gets right to point   
Name: Nikhil
Date: 2007-09-21 8:49:58 AM
Comment:
Helpful and smart teaching
Title: Helpful Article   
Name: Arshi Bano
Date: 2007-08-19 8:00:54 AM
Comment:
Simple and helpful solution
Title: Great stuff   
Name: Bill
Date: 2007-08-18 4:05:08 PM
Comment:
As a Newbie to App Development, this awesome!
Title: Good Job   
Name: Prabu Ram
Date: 2007-07-20 6:02:46 AM
Comment:
Nice and neat and to the point.
Title: ITS REALLY NICE.   
Name: AMBAR PARDESI
Date: 2007-07-05 4:00:19 AM
Comment:
SHORT N SIMPLE TO UNDERSTAND.
Title: good article   
Name: jayanta
Date: 2007-06-29 6:02:19 AM
Comment:
good example
Title: Clear - cut explanation   
Name: ravi
Date: 2007-06-08 4:20:41 AM
Comment:
good example by aspallince
all matters should be expained in such manner
Title: Good Article   
Name: Sri Harsha
Date: 2007-05-17 9:07:17 AM
Comment:
Very nice article
Title: Simple and clear   
Name: Ashi
Date: 2007-05-08 3:06:46 AM
Comment:
Brief, to the point and beautifully explained the fundamental basic.
Title: good   
Name: Dan
Date: 2007-05-03 9:38:00 AM
Comment:
Very clear article, thanks so much.
Title: best solution i have   
Name: Alpesh Donga
Date: 2007-04-22 2:22:52 AM
Comment:
There can be simple solution.
Title: Helpful   
Name: johora
Date: 2007-04-18 5:18:57 AM
Comment:
thanks for your helpful topics. its realy good
Title: very good   
Name: manish
Date: 2007-03-18 7:14:39 AM
Comment:
hi thanx
Nice Explanation






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:08:24 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search