Back To Basics: Constructor and it's Types in C#

Constructor is a special method which automatically invokes at the time of instantiation. It is a special method because it do not have a return type and named as name of class.

Constructor initializes the fields of the class and members of new object. A class has at least one constructor. If there is no constructor in the class, then the compiler will automatically invoke default constructor.

Constructor without parameter is known as default constructor.

Types of Constructors:
  • Default Constructor
  • Parameterized Constructor
  • Static Constructor
  • Private Constructor
Default Constructor

Constructor without parameter is known as default constructor. It initializes all numeric fields in a class to zero and all string and object fields to null.

Parameterized Constructor

Constructor with at least one parameter is known as Parameterized constructor, so that it can initialize each instance of the class to different values.

Static Constructor

Static constructor is used to initialize static fields of the class, in static field we write the code that is required to execute only once because static constructor gets invoked only once. It is called at the time of creation of first instance of class by CLR.

Private Constructor

Private constructor is generally used in class with static members only. If a class has only private constructor, other classes cannot create instance of this class.

Code Snippet:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace BackToBasics  
  7. {  
  8.     class DefaultConstr  
  9.     {  
  10.         public string val1, val2;  
  11.         public DefaultConstr()//Default Constructor  
  12.         {  
  13.             //Console.WriteLine("Default Constructor");  
  14.             val1 = "iShriss";  
  15.             val2 = "Sharma";  
  16.         }  
  17.     }  
  18.   
  19.     class ParameterizedConstr  
  20.     {  
  21.         public string para1, para2;  
  22.         public ParameterizedConstr(string name, string surname)//Parameterized constructor  
  23.         {  
  24.             para1 = name;  
  25.             para2 = surname;  
  26.         }  
  27.     }  
  28.   
  29.     class StaticConstr  
  30.     {  
  31.         public string value1, value2;  
  32.   
  33.         static StaticConstr() //Static constructor  
  34.         {  
  35.             //initialize field that needs to be execute only one  
  36.             Console.WriteLine("This is Static constructor.Static constr invoked only once.!");  
  37.         }  
  38.   
  39.         public StaticConstr()  
  40.         {  
  41.             value1 = "Instance constructor invoked everytime when object is created.";  
  42.             value2 = "This is instance(default) constructor";  
  43.         }  
  44.     }  
  45.   
  46.     class PrivateConstr  
  47.     {  
  48.         public string fname, lname;  
  49.         public PrivateConstr(string Firstname, string Lastname)  
  50.         {  
  51.             fname = Firstname;  
  52.             lname = Lastname;  
  53.         }  
  54.   
  55.         private PrivateConstr()//private constructor  
  56.         {   
  57.             Console.WriteLine("This is private constr which retrict instantiation.");   
  58.         }  
  59.     }  
  60.   
  61.     class Program  
  62.     {  
  63.         static void Main(string[] args)  
  64.         {  
  65.   
  66.             DefaultConstr obj = new DefaultConstr(); // Default constructor  
  67.   
  68.             Console.WriteLine("Default Constructor:"+Environment.NewLine);  
  69.             Console.WriteLine(obj.val1);// Output:iShriss  
  70.             Console.WriteLine(obj.val2); //Output:Sharma  
  71.             Console.WriteLine(Environment.NewLine);  
  72.   
  73.             Console.WriteLine("Parameterized Constructor:"+Environment.NewLine);  
  74.   
  75.             ParameterizedConstr objParameter = new ParameterizedConstr("Shridhar""Sharma"); //Parameterized Constructor  
  76.             Console.WriteLine(objParameter.para1+","+objParameter.para2);//Output:Shridhar,Sharma  
  77.             Console.WriteLine(Environment.NewLine);  
  78.   
  79.             Console.WriteLine("Static Constructor:"+Environment.NewLine);  
  80.               
  81.             // At this time both constructors(Static constr(invoke once) and instance(default) constructor invoked)  
  82.             StaticConstr objStatic = new StaticConstr();//Static and Instance constructor invoked.  
  83.             Console.WriteLine("Both constructors will invoked."+Environment.NewLine);  
  84.             Console.WriteLine(objStatic.value1);  
  85.             Console.WriteLine(objStatic.value2);  
  86.             Console.WriteLine(Environment.NewLine);  
  87.   
  88.             Console.WriteLine("Private Constructor:"+Environment.NewLine);  
  89.   
  90.             //PrivateConstr objPrivateCtr = new PrivateConstr();//Not allowed because of private cnstr  
  91.   
  92.             PrivateConstr objPrivate = new PrivateConstr("iShriss""Sharma"); //will execute  
  93.             Console.WriteLine(objPrivate.fname + "," + objPrivate.lname);//Output:Shridhar,Sharma  
  94.             Console.ReadKey();  
  95.         }          
  96.     }  
  97.     
  98. }  
Output

 

Closure

By going through this article we understood the basic definition and implementation of various types of constructors.