Tuesday, September 21, 2010

Understanding the Destructors and Static constructors in C#

A destructors is a member that implements the actions required to destruct an instance of a class. Destructors cannot have parameters, cannot have accessibility modifiers, and cannot be called explicitly. The destructor for an instance is called automatically during garbage collection. The .NET framework has an in built mechanism called Garbage Collection to de-allocate memory occupied by the un-used objects. The destructor implements the statements to be executed during the garbage collection process. A destructor is a function with the same name as the name of the class but starting with the character ~.

Example: -We have three classes A, B and C. B is derived from A, and C is derived from B. Each class has their own constructors and destructors. In the main of the class App, we create an object of C.
using System;
class A
{
    public A()
    {
        Console.WriteLine("Creating A");
    }
    ~A()
    {
        Console.WriteLine("Destroying A");
    }
}

class B : A
{
    public B()
    {
        Console.WriteLine("Creating B");
    }
    ~B()
    {
        Console.WriteLine("Destroying B");
    }

}
class C : B
{
    public C()
    {
        Console.WriteLine("Creating C");
    }

    ~C()
    {
        Console.WriteLine("Destroying C");
    }
}
class App
{
    public static void Main()
    {
        C c = new C();
        Console.WriteLine("Object Created ");
        Console.WriteLine("Press enter to Destroy it");
        Console.ReadLine();
        c = null;
        //GC.Collect();

        Console.Read();
    }

}

Output is display below.


Understanding the Static constructors in C#
A Static constructor is a member that implements the actions required to initialize a class Static constructors cannot have parameters, cannot have accessibility modifiers, and cannot be called explicitly. This static constructor for a class is called automatically. The normal constructors, which we explained till now, can be used for the initialization of both static and non-static members. But C# provides a special type of constructor known as static constructor to initialize the static data members when the class is loaded at first. Remember that, just like any other static member functions, static constructors can't access non-static data members directly. 
The name of a static constructor must be the name of the class and even they don't have any return type. The keyword static is used to differentiate the static constructor from the normal constructors. The static constructor can't take any arguments. That means there is only one form of static constructor, without any arguments. In other way it is not possible to overload a static constructor. 

Example: -

public class Bus
{
    // Static constructor:
    static Bus()
    {
        System.Console.WriteLine("The static constructor invoked.");
    }

    public static void Drive()
    {
        System.Console.WriteLine("The Drive method invoked.");
    }
}

class TestBus
{
    static void Main()
    {
        Bus.Drive();
    }
}

Note: - In this example, the class Bus has a static constructor and one static member, Drive (). When Drive () is called, the static constructor is invoked to initialize the class.

Output is display below. 




2 comments:

vijay said...

Very nice article and helpfull for me.

Rakesh said...

good article

Recent Posts