Constructors have a very special meaning to the compiler and CLR but sometimes its flow seems difficult for a developer. This article's explanation is simple but provides important insights into Constructors.
So, What is a Constructor?
A Constructor is a special method in a class/struct with the same name as the class/struct without any return type that initializes fields and members of a class/struct.
A constructor can only be called by:
- The Compiler using the New keyword to initialize a class/struct.
- Another constructor of the same class/struct using the this keyword.
- Constructors of the derived class using the base() keyword.
Types of Constructors in C#
The following are the types of Constructors in C#:
- Default constructor.
- Parameterized Constructor.
- Instance Constructor.
- Static Constructor.
- Private Constructor.
Default Constructors
The following describes Default Constructors:
- A Constructor with no parameter is called a Default Constructor.
- A Default Constructor is called by the compiler when no arguments are passed to the New operator while creating an object of a class or struct.
- If there is no constructor in a non-static class then a Public Default Constructor is provided by the 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 the compiler to initialize each field of the struct to its default value.
Parameterized Constructors
The following describes Parameterized Constructors:
-
A constructor with parameters is called a parameterized Constructor.
-
A class or struct can have multiple parameterized constructors as long as they have a different method signature. They follow the same concept as method overloading.
-
The 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.
Static Constructors
The following describes Static Constructors:
- To initialize a Static Class or static variables in a 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 Constructor per class
- Static Constructors cannot have an explicit "this" or "base" constructor call, in other words 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
Private Constructors
The following describes Private Constructors:
- A constructor becomes a private constructor when we declare it with the private access specifier.
- Private Constructors can neither be instantiated nor inherited from another class.
- An object of a class can only be created in the class itself.
- Microsoft recommends its use for the implementation of the Singleton Pattern.
Constructor Chaining
The following describes Constructor Chaining:
- A constructor can call another constructor of the same class or of the base class
- Since one constructor can invoke another, this sometimes can cause the execution of multiple constructors; that is referred to as Constructor Chaining
- If a class is not derived from any other class then the following would be the chain:
- Static Constructor.
- Instance Constructor
- If a class is derived from any other class then the following would be the chain:
- Derived Static Constructor.
- Base Static Constructor.
- Base Instance Constructor.
- Derived Instance Constructor.
I hope all topics related to constructors have been covered. Please let me know if I have missed any or you need more explanations or examples.
!! Happy Programming !!