Working with normal and static constructor in C# 2.0
page 2 of 3
by Uday Denduluri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 21817/ 182

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.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 5 and type the answer here:

User Comments

Title: Simple and Clear   
Name: Shine T Jose
Date: 10/24/2008 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: 8/4/2008 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: 7/28/2008 2:12:26 AM
Comment:
simple and easy to understand.
Title: Sweet   
Name: Maneesh
Date: 7/9/2008 2:50:23 AM
Comment:
Short and Sweet Article about static constructor...
Title: Fantastic....   
Name: Hariprasad seelam
Date: 7/1/2008 7:56:51 AM
Comment:
Heart full explanation.thanks
Title: .   
Name: .
Date: 6/13/2008 7:33:18 AM
Comment:
Good job mate
Title: thx   
Name: asha
Date: 6/4/2008 7:32:43 AM
Comment:
it is a very useful info thx
Title: thanx alot   
Name: kouky
Date: 5/30/2008 2:45:00 PM
Comment:
thanx for this article
Title: very good   
Name: gadhvi
Date: 5/5/2008 9:23:16 AM
Comment:
it's really very helpfull
Title: Working with normal and static constructor in C# 2.0   
Name: Ilavarasi
Date: 2/29/2008 11:19:34 PM
Comment:
Very Good Article.Great job
Title: Good Explanation   
Name: Sri
Date: 1/19/2008 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: 12/5/2007 12:48:13 AM
Comment:
its simple to understand
Title: Gets right to point   
Name: Nikhil
Date: 9/21/2007 8:49:58 AM
Comment:
Helpful and smart teaching
Title: Helpful Article   
Name: Arshi Bano
Date: 8/19/2007 8:00:54 AM
Comment:
Simple and helpful solution
Title: Great stuff   
Name: Bill
Date: 8/18/2007 4:05:08 PM
Comment:
As a Newbie to App Development, this awesome!
Title: Good Job   
Name: Prabu Ram
Date: 7/20/2007 6:02:46 AM
Comment:
Nice and neat and to the point.
Title: ITS REALLY NICE.   
Name: AMBAR PARDESI
Date: 7/5/2007 4:00:19 AM
Comment:
SHORT N SIMPLE TO UNDERSTAND.
Title: good article   
Name: jayanta
Date: 6/29/2007 6:02:19 AM
Comment:
good example
Title: Clear - cut explanation   
Name: ravi
Date: 6/8/2007 4:20:41 AM
Comment:
good example by aspallince
all matters should be expained in such manner
Title: Good Article   
Name: Sri Harsha
Date: 5/17/2007 9:07:17 AM
Comment:
Very nice article
Title: Powerful   
Name: Howard
Date: 5/15/2007 2:25:05 PM
Comment:
Very succinct and technically cathartic.
Title: Simple and clear   
Name: Ashi
Date: 5/8/2007 3:06:46 AM
Comment:
Brief, to the point and beautifully explained the fundamental basic.
Title: good   
Name: Dan
Date: 5/3/2007 9:38:00 AM
Comment:
Very clear article, thanks so much.
Title: best solution i have   
Name: Alpesh Donga
Date: 4/22/2007 2:22:52 AM
Comment:
There can be simple solution.
Title: Helpful   
Name: johora
Date: 4/18/2007 5:18:57 AM
Comment:
thanks for your helpful topics. its realy good
Title: very good   
Name: manish
Date: 3/18/2007 7:14:39 AM
Comment:
hi thanx
Nice Explanation

Product Spotlight
Product Spotlight 






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


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 6:06:28 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search