All About Constructors In C#

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:

  1. class Constructor  
  2. {  
  3.     public Constructor()  
  4.     {  
  5.   
  6.     }  
  7. }  
Types of Constructors:

 

  1. Default
  2. Parameterized
  3. Private
  4. 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

  1. namespace Constructors  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {}  
  7.     }  
  8.   
  9.     class Constructor  
  10.     {  
  11.         string name = string.Empty;  
  12.         public Constructor() // Default Constructor  
  13.             {  
  14.                 name = "Nishant";  
  15.             }  
  16.     }  
  17. }  
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”.
  1. namespace Constructors  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {}  
  7.     }  
  8.   
  9.     class Constructor  
  10.     {  
  11.         string name = string.Empty;  
  12.         public string Name  
  13.         {  
  14.             get;  
  15.             set;  
  16.         }  
  17.         public Constructor() // Default Constructor  
  18.             {  
  19.                 name = "Nishant";  
  20.             }  
  21.         public Constructor(string name) //Parameterized Constructor  
  22.             {  
  23.                 this.Name = name;  
  24.             }  
  25.     }  
  26. }  
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.

Private Constructor

program

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.
  1. class Constructor  
  2. {  
  3.     public static int counter;  
  4.   
  5.     static Constructor()  
  6.     {  
  7.         counter = 1;  
  8.     }  
  9. }  
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,
  1. namespace Constructors  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.   
  8.             C objC = new C();  
  9.         }  
  10.     }  
  11.   
  12.     class A  
  13.     {  
  14.         public A();  
  15.     }  
  16.     class B: A  
  17.     {  
  18.         public B();  
  19.     }  
  20.     class C: B  
  21.     {  
  22.         public C();  
  23.     }  
  24. }  
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:
  1. namespace Constructors  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.   
  8.   
  9.             A objA = new B();  
  10.             B objB = objA; //Will this work??  
  11.         }  
  12.     }  
  13.   
  14.     class A  
  15.     {  
  16.         public A();  
  17.     }  
  18.     class B: A  
  19.     {  
  20.         public B();  
  21.     }  
  22.     class C: B  
  23.     {  
  24.         public C();  
  25.     }  
  26. }  
Solution: No, it will give you compile time exception.

Code