Constructor And Destructor in C#

Constructor

A constructor looks like a special method having no return type and its name is same with the class name.

If we do not declare constructor explicitly for a class, compiler will create a default constructor at run time.

Access modifiers of constructor can be public, private, protected, internal and extern.

  1. A public constructor is called when the class is instantiated.
  2.  A private constructor prevents the creation of the object of the class. It is used in classes having only static members.
  3. A class having internal constructor cannot be instantiated outside of the assembly.
  4. A protected constructor of base class can only be called from the derived class within derived class constructor.
  5. When a constructor having an extern modifier, the constructor is said to be an external constructor, because the same does not provide any actual implementation.

Types of Constructor

  1. Static Constructor and
  2. Instance Constructor

Static Constructor

Static constructors are called before the first instance of the class is created or any static members are accessed or used whichever is earlier.

Features of static constructor:

  • A Static constructor cannot be overloaded.
  • There is no access modifier in Static constructor and they don't take any arguments also.
  • The static constructor for a class executes only once throughout the life time of the program.
  • Static constructor does not support Constructor Channing.
  • Static constructor can call only static members.
  • Static constructor is getting called before instance constructor

//The static constructor for a class executes only once throughout the life time of the program

class CherukuriClass
{
   
public CherukuriClass()
    {
       
Console.WriteLine("Instance Constructor called");
    }

    static CherukuriClass()
    {
       
Console.WriteLine("Static Constructor called");
    }
}

class Program
{
   
static void Main(string[] args)
    {
       
CherukuriClass objClass1 = new CherukuriClass();
       
CherukuriClass objClass2 = new CherukuriClass();
    }
}

//Static constructor is getting called before instance constructor

class CherukuriClass
{
   
public CherukuriClass()
    {
       
Console.WriteLine("CherukuriClass Default Constructor");
    }

    static CherukuriClass()
    {
       
Console.WriteLine("CherukuriClass Static Constructor");
    }
}

class CherukuriChildClass : CherukuriClass
{
   
public CherukuriChildClass()
    {
       
Console.WriteLine("CherukuriClass Child Constructor");
    }
}

class Program
{
   
static void Main(string[] args)
    {
       
CherukuriChildClass objChild = new CherukuriChildClass();
    }
}

 

Instance Constructor

Instance constructor is used to create and initialize instance and it is invoked when we create a new object of the class.

 


Features of Constructor

 


  1. Constructor cannot be virtual. When constructor is invoked the virtual table would not be available in the memory.
  2. Constructor Chaining
    Constructor chaining means call one constructor from another constructor. In order to call one constructor from another, we use base (parameters) or: this (parameters)

We use base (parameters) to call a constructor in the base class and we use this (parameters) to call a constructor in the current class.

 

3.    Inheritance
A constructor of a base class cannot be inherited to its derived class. But base class constructor or parent class constructor can be invoked from derived class or child class.

 

class CherukuriClass
{
   
public CherukuriClass()
    {
       
Console.WriteLine("CherukuriClass with Default Constructor");
    }

    public CherukuriClass(int x)
    {
       
Console.WriteLine("CherukuriClass Constructor With Single Parameter");
    }
}

class CherukuriChildClass : CherukuriClass
{
   
public CherukuriChildClass(): base(10)
    {
       
Console.WriteLine("CherukuriChildClass Constructor");
    }
}

class Program
{
   
static void Main(string[] args)
    {
       
CherukuriChildClass objChild = new CherukuriChildClass();
    }
}

 

 

4.    Overloading
Constructor can be overloaded. In the following code snippet, there are four overloaded constructor:

 

public CherukuriClass() //Default constructor without parameter.
{

 

}

public CherukuriClass(int UserID) //Constructor overloading.
{

 

}

Destructor

Destructors are used to destruct instances of classes. Following are features of destructor:

  • A class can have one destructor only.
  • Destructors cannot be inherited or overloaded.
  • Destructors are invoked automatically.
  • Destructor can not have modifiers or parameters.
  • When destructor is called, Finalize is called from destructor implicitly.

 

When derived class destructor is called, the base class destructor is also getting called.

class CherukuriClass

{

    ~CherukuriClass()  // Destructor

    {

        //  Here we can write code for cleanup resources

    }

}

 

 

Note

  • Base constructor is getting called first. In general, destructors are called in the reverse order of the constructor calls.
  • For example, if class A is the base class and class B inherit class A and class C inherit class B. Constructor will be fired for class A first, class B second and at last for class C.
  • But destructor will be fired in reverse order: class C first, class B second and at last for class A.