AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1027&pId=-1
Working with normal and static constructor in C# 2.0
page
by Uday Denduluri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 31684/ 26

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.



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