All About Constructor In C#

A constructor is a special type of method of a C# class which invokes automatically when a new instance of a class is created. Constructor is used in object initialization and memory allocation of the class. Constructor is used to initialize private fields and their values of the class. A constructor can be overloaded.

When we don’t create constructor for the class, the compiler automatically create a default constructor for the class. Constructor name is always same of the class name.

about Constructor

General Syntax for Constructor


[Access Modifier] ClassName([Parameters])
{
}

Types of Constructors in C# 

  • Default Constructor
  • Parameterized Constructor
  • Static Constructor
  • Private Constructor

Constructor
 

Default Constructor in C#


A constructor which does not have any parameters or we can say without any parameters is called default constructor. It initializes the same value of every instance of class.
  1. using System;  
  2. namespace ConstructorExample  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         public string EmployeeName;  
  7.         public string EmployeeAddress;  
  8.         //Default Constructor  
  9.         public Employee()  
  10.         {  
  11.             EmployeeName = "Mukesh Kumar";  
  12.             EmployeeAddress = "New Delhi";  
  13.         }  
  14.     }  
  15.     class Program  
  16.     {  
  17.         static void Main(string[] args)  
  18.         {  
  19.             Employee emp = new Employee();  
  20.             Console.WriteLine("Name of Employee :" + emp.EmployeeName);  
  21.             Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);  
  22.             Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  

Parameterized Constructor


A constructor that has at least one parameter is called a Parameterized Constructor. Using this type of constructor, we can initialize each instance of the class with different values.
  1. using System;  
  2. namespace ConstructorExample  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         public string EmployeeName;  
  7.         public string EmployeeAddress;  
  8.         //Parameterized Constructor  
  9.         public Employee(string name, string address)  
  10.         {  
  11.             EmployeeName = name;  
  12.             EmployeeAddress = address;  
  13.         }  
  14.     }  
  15.     class Program  
  16.     {  
  17.         static void Main(string[] args)  
  18.         {  
  19.             Employee emp = new Employee("Mukesh Kumar""New Delhi");  
  20.             Console.WriteLine("Name of Employee :" + emp.EmployeeName);  
  21.             Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);  
  22.             Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  

Static Constructor


A static constructor is used to initialize any type of static data of the class or perform action need to be performed only once. A Static Constructor is called automatically before the first instance of the class or any static data is referenced. It is called only once for any number of classes instance is created.
  1. using System;  
  2. namespace ConstructorExample  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         public static readonly long Baseline;  
  7.         public string EmployeeName;  
  8.         public string EmployeeAddress;  
  9.         //Static Constructor  
  10.         static Employee()  
  11.             {  
  12.                 Baseline = DateTime.Now.Ticks;  
  13.                 Console.WriteLine("Static constructor executes first");  
  14.             }  
  15.             //Default Constructor  
  16.         public Employee()  
  17.         {  
  18.             EmployeeName = "Mukesh Kumar";  
  19.             EmployeeAddress = "New Delhi";  
  20.             Console.WriteLine("Executes after static constructor");  
  21.         }  
  22.     }  
  23.     class Program  
  24.     {  
  25.         static void Main(string[] args)  
  26.         {  
  27.             Employee emp = new Employee();  
  28.             Console.WriteLine("Name of Employee :" + emp.EmployeeName);  
  29.             Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);  
  30.             Console.ReadLine();  
  31.         }  
  32.     }  
  33. }  
Key point of static constructor
  • Only one static constructor can exist in a class.
  • It doesn’t take any parameter because of it automatically called by the CLR or access modifier.
  • It is called automatically before the first instance of the class created.
  • We cannot call static constructors directly.

When to use static constructors

Static constructors are very useful when you create wrapper classes for unmanaged code. You can also use it when class is using log file and constructor is used to write entries.

Private Constructor


A private constructor is a special constructor that generally used in class that contains only static members. If class has only private constructors not any public constructor then it is not possible to create instance of the class. Basically private constructor prevents to create instance of the class. If you want to create instance of the class which has private constructor then you need to create a public constructor along with private constructor.
  1. using System;  
  2. namespace ConstructorExample  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         private Employee()  
  7.         {}  
  8.         public static int currentEmployee;  
  9.         public static int IncreamentEmployee()  
  10.         {  
  11.             return ++currentEmployee;  
  12.         }  
  13.     }  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             Employee.currentEmployee = 50;  
  19.             Console.WriteLine("Current Employee is :" + Employee.currentEmployee);  
  20.             Console.WriteLine("Increament Employee is " + Employee.IncreamentEmployee());  
  21.             Console.ReadLine();  
  22.         }  
  23.     };  
  24. }  
Thanks for reading this article. Hope you enjoyed it.


Similar Articles