Tuesday, April 21, 2009

Working with instance and static constructor in C# 2.0

Abstract

In this article, i am gonna tell u about the static and non-static constructor. After going through this snippet, reader gets a clear cut understanding of the steps involved in object initialization (Both static and non-static).

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.

Code Snippet

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

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();
}
}

Figure 1

In the above screen,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.

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


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();
}
}

Figure 2

Surprised witht this output. This is the obevious behaviour of this. 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.