Let’s starts with the basic things what constructor is and what role it plays during object creation.
Definition
Constructor is a method in A class which will get executed when its object is created. We also call this as a special method of the class.
In case we don’t provide any constructor in the class, the compiler will create a default constructor for that particular class. So by default there will be one constructor of the class and it’s mandatory.
This is how we create a constructor of a class:
- class Constructor
- {
- public Constructor()
- {
-
- }
- }
Types of Constructors:
- Default
- Parameterized
- Private
- Static
Default Constructor
A constructor without any parameter is called default constructor. In case we won’t define any constructor in a class, compiler will create a default constructor.
Example of Default Constructor
- namespace Constructors
- {
- class Program
- {
- static void Main(string[] args)
- {}
- }
-
- class Constructor
- {
- string name = string.Empty;
- public Constructor()
- {
- name = "Nishant";
- }
- }
- }
Parameterized Constructor In case we pass any parameter in constructor we call it as parameterized constructor.
In the following example we are passing “
name” parameter in the constructor and assigning it to property of the class called “
Name”.
- namespace Constructors
- {
- class Program
- {
- static void Main(string[] args)
- {}
- }
-
- class Constructor
- {
- string name = string.Empty;
- public string Name
- {
- get;
- set;
- }
- public Constructor()
- {
- name = "Nishant";
- }
- public Constructor(string name)
- {
- this.Name = name;
- }
- }
- }
Private Constructor When you don’t want object of the class to be created and can’t be inherited, so private constructor is a perfect choice.
In the above screenshot I have tried to access the private constructor, but I won’t get a chance to create an object of the same. One is showing as default and another is showing with the string parameter.
Note: While implementing singleton pattern we generally use private constructor.
Static Constructor I must say it’s a special type of constructor. The reason for the same is any developer can’t persist the state of static constructor. It will be invoked only once for any number of instances of the class and it’s during the creation of first instance of the class or the first reference to a static member in the class.
In the following example I have just tried to initialize the static counter to 1.
- class Constructor
- {
- public static int counter;
-
- static Constructor()
- {
- counter = 1;
- }
- }
Let us say you have static logger object, the best way to initialize the same is in static constructor.
Constructor Behavior in Inheritance
Look at the following example,
- namespace Constructors
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- C objC = new C();
- }
- }
-
- class A
- {
- public A();
- }
- class B: A
- {
- public B();
- }
- class C: B
- {
- public C();
- }
- }
Question: When I will create an object of Class C what will happen?
Solution: When you will debug the same you can find the actual output. It’s interesting too. First “
Class A” constructor will get called, then B and finally C.
The reason being to create a child is that first parents should exist.
Look at the sample code below:
- namespace Constructors
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- A objA = new B();
- B objB = objA;
- }
- }
-
- class A
- {
- public A();
- }
- class B: A
- {
- public B();
- }
- class C: B
- {
- public C();
- }
- }
Solution: No, it will give you compile time exception.