Constructor in .NET

Constructors have a very special meaning to Compiler and CLR but some times its meaning and flow seems difficult for a developer. Today I will explain simple but important insights of Constructor.

So, What is a Constructor?


A Constructor is a special method in a class/struct with the same name as of class/struct without any return type, used to initialized fields and members of a class/struct; 

A constructor can only be called by:

  • Compiler using New keyword to initialize a class/struct. 
  • Another constructor of same class/struct using :this() keyword.
  • Constructors of derived class using :base() keyword.

Types of Constructor?

  • Default constructor.
  • Parameterized Constructor.
  • Instance Constructor.
  • Static Constructor.
  • Private Constructor.
  • Copy Constructor.

Default Constructor?
  1. A Constructor with no parameter is called Default Constructor.
  2. Default Constructor is called by compiler when no arguments are passed to New operator while creating a object of class or Struct.
  3. If there is no constructor in a Non-Static class, a Public Default Constructor is provided by compiler so that a class can be instantiated.
  4. A Struct cannot have an explicit Default Constructor(We can not define an explicit Default Constructor in a struct), but it is always provided by compiler to initialize each field of struct to its default value.

Parameterized Constructor?

  1. A constructor with parameters are called parameterized Constructor.
  2. A Class or Struct can have multiple parameterized constructors as long as they have different method signature. They follow same concept of method overloading.
  3. Compiler provides Default Constructors only if there is no constructor(Default or Parameterized) defined in a class.
  4. Parameterized Constructors can exists even without the existence of Default Constructors.

Static Constructor?

  1. To initialize a Static Class or Static variables in Non-Static Class, Static Constructors are used; 
  2. Access Modifiers are not allowed on Static Constructors; 
  3. Static Constructors cannot be parameterized; 
  4. There can be only one Static Constructors per class; 
  5. Static Constructors cannot have an explicit 'this' or 'base' constructor call. i.e Static Constructors cannot be called directly; 
  6. Static Constructors are called automatically before first instance of a class is created or any static member is referenced; 
  7. Static Constructors are called called only once in lifetime of a class;

Private Constructor?


  1. A constructors becomes private constructor when we declare with private access specifier. 
  2. Private Constructors can neither be instantiated nor inherited from other class. 
  3. Object of class can only be created in the class itself. 
  4. Microsoft recommends its uses to implement Singleton Pattern.

Copy Constructor?

A parameterized constructor that contains a parameter of same class type is called as copy constructor. It can be used to initialize a new class from an existing class of same type.

Constructor Chaining?
  1. A constructor can make call to another constructor of same class or of base class;
  2. Since one constructor can invoke other, this sometimes can cause execution of multiple constructors and referred to as Constructor chaining;
  3. If class is not derived from any other class, below would be the chain:
    1. Static Initializer.
    2. Static Constructor.
    3. Instance Initializer.
    4. Instance Constructor
  4. If a class is derived from any other class, below would be the chain:
    1. Derived Static Initializer.
    2. Derived Static Constructor.
    3. Derived Instance Initializer.
    4. Base Static Initializer.
    5. Base Static Constructor.
    6. Base Instance Initializer.
    7. Base Instance Constructor.
    8. Derived Instance Constructor.

I hope i have covered all topics related to constructor. Please let me know if i have missed any or you need more explanation or examples.

!! Happy Programming !!