Constructors have a very special meaning to Compiler and CLR but sometimes its 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 that of class/struct without any return type, used to initialize 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
- Constructors of derived class using
Types of Constructor in C#?
- Default constructor
- Parameterized constructor
- Instance constructor
- Static constructor
- Private constructor
- A Constructor with no parameter is called Default Constructor.
- Default Constructor is called by compiler when no arguments are passed to
New operator while creating an object of class or struct.
- 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.
- A Struct cannot have an explicit Default Constructor (We cannot 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.
- A constructor with parameters is called parameterized Constructor.
- A Class or Struct can have multiple parameterized constructors as long as they have different method signature. They follow the same concept of method overloading.
- Compiler provides Default Constructors only if there is no constructor (Default or Parameterized) defined in a class.
- Parameterized Constructors can exist even without the existence of Default Constructors.
- To initialize a Static Class or Static variables in Non-Static Class, Static Constructors are used
- Access Modifiers are not allowed on Static Constructors
- Static Constructors cannot be parameterized
- There can be only one Static Constructors per class
- Static Constructors cannot have an explicit '
this' or '
base' constructor call, i.e., Static Constructors cannot be called directly
- Static Constructors are called automatically before the first instance of a class is created or any static member is referenced
- Static Constructors are called only once in the lifetime of a class
- A constructor becomes a
private constructor when we declare with
private access specifier.
- Private Constructors can neither be instantiated nor inherited from other class.
- Object of class can only be created in the class itself.
- Microsoft recommends its uses to implement Singleton Pattern.
- A constructor can make a call to another constructor of the same class or of base class;
- Since one constructor can invoke another, this sometimes can cause execution of multiple constructors and is referred to as Constructor chaining;
- If class is not derived from any other class, below would be the chain:
- Static Constructor
- Instance Constructor
- If a class is derived from any other class, below would be the chain:
- Derived Static Constructor
- Base Static Constructor
- Base Instance Constructor
- Derived Instance Constructor
I hope I have covered all topics related to constructor. Please let me know if I have missed any or if you need more explanation or examples.
!! Happy programming !!