Constructor is the special type of method of a class which invoke automatically when instance of class is created.  Constructor is used to object initialization and memory allocation of the class. Constructor is used to initialize private fields value of the class whenever instance or object of class is created. 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

General Syntax for Constructor

[Access Modifier] ClassName([Parameters])
{
}
Types of Constructor

Default Constructor

Parameterized Constructor

Static Constructor

Private Constructor

Default Constructor

A constructor which has not defined any parameters or we can say without any parameters is called default constructor. It initializes the same value of every instance of class.

using System;
namespace ConstructorExample
{
    public class Employee
    {
        public string EmployeeName;
        public string EmployeeAddress;

        //Default Constructor
        public Employee()
        {
            EmployeeName = "Mukesh Kumar";
            EmployeeAddress = "New Delhi";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            Console.WriteLine("Name of Employee :" + emp.EmployeeName);
            Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);
            Console.ReadLine();
        }
    }
}
Parameterized Constructor

A constructor which has at least one parameter is called Parameterized Constructor. Using this type of constructor we can initialize each instance of the class to different values.

using System;
namespace ConstructorExample
{
    public class Employee
    {
        public string EmployeeName;
        public string EmployeeAddress;       

        //Parameterized Constructor
        public Employee(string name, string address)
        {
            EmployeeName = name;
            EmployeeAddress = address;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee("Mukesh Kumar","New Delhi");
            Console.WriteLine("Name of Employee :" + emp.EmployeeName);
            Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);
            Console.ReadLine();
        }
    }
}
Static Constructor

Static constructor is used to initialize any type of static data of the class or perform action that need to be performed only once. Static Constructor is called automatically before the first instance creates of the class or any static data is referenced. It is called only once for any number of class instance  is created.

using System;
namespace ConstructorExample
{
    public class Employee
    {
        public static readonly long Baseline;
        public string EmployeeName;
        public string EmployeeAddress;   
        

        //Static Constructor
        static Employee()
        {
            Baseline = DateTime.Now.Ticks;
            Console.WriteLine("Static constructor executes first");
        }

        //Default Constructor
        public Employee()
        {
            EmployeeName = "Mukesh Kumar";
            EmployeeAddress = "New Delhi";
            Console.WriteLine("Executes after static constructor");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            Console.WriteLine("Name of Employee :" + emp.EmployeeName);
            Console.WriteLine("Address of Employee :" + emp.EmployeeAddress);
            Console.ReadLine();
        }
    }
}
Key point of static constructor

Only one static constructor can be created in the class.

It doesn’t take any parameter because of it automatically called by CLR or access modifier.

It is called automatically before the first instance of the class creates.

We can not call static constructor directly.

When to use static constructor

Static constructor is 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

Private constructor is special constructor that generally used in class that contain only static members. If class has only private constructors not any public constructors than 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.

using System;
namespace ConstructorExample
{
    public class Employee
    {
        private Employee()
        {
        }
        public static int currentEmployee;
        public static int IncreamentEmployee()
        {
            return ++currentEmployee;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee.currentEmployee = 50;
            Console.WriteLine("Current Employee is :" + Employee.currentEmployee);
            
            Console.WriteLine("Increament Employee is " + Employee.IncreamentEmployee());
            Console.ReadLine();
        }
    };
}
Conclusion:

So, today we learned what is constructor in C# and how to use it. We also learned about types of constructor one by one with example.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks