Constructors in C#

 

Constructor in C# is a special method that is available to a class responsible for initializing the variables of a class. A constructor is mandatory for a class if we want to create a object for the class. Because constructor is mandatory for a class to create an object it must either be defined explicitly or will be defined implicitly by the compiler while compiling a program provided it is not defined explicitly. 

Whenever an instance of a class is allocated, the compiler automatically executes a special method called a constructor. The purpose of the constructor is to initialize the fields within a class. It is common to use a constructor to initialize new reference types, as well as to pass existing objects to be acted on by the class.

 

Defining a constructor is optional but typically recommended. Also, you may overload the constructor with as many methods as needed, as long as the method signature is unique. If you do create your own constructor, make sure that you also create a default constructor that takes no parameters.

 

A constructor signature requires that the name of the constructor be the name of the class and that the method contains no return value. The variable part is the number and order of the parameters. This is where you have the power to overload the constructor.

 

Note If you choose to write your own constructor, you must provide at least a minimum method definition for the constructor, if you wish to use a simulated default constructor.

 

The name of constructor method will be same as the class name and constructor will not have any return types. That means they are non-value returning methods.

class Employee 

{ 

   int i; 

   string str; 

} 

As shown above we have Employee class and after compilation, compiler will generate implicit Employee()constructor which initialize the variables as shown below.  

class Employee 

{ 

       int i; 

       string str;

 

       public Employee() 

       { 

               i = 0;

               str = null; 

       } 

} 

Implicitly constructors are always public. Even you can define constructor yourself as shown below. 

class Employee 

{ 

      public Employee() 

      { 

     

      } 

} 

Every method in a class has to be explicitly called if we need to be execute. As constructor also normal method, we have to call the constructor. C# class constructor will execute whenever we create the object for the class. 

Constructors are two types. Those are Parameter-less constructor(default constructor) and Parameterized constructor.  

If constructor not requires any parameters we will call it as Parameter-less constructor. If constructor requires any parameters then it will has parameterized constructor. Implicit constructor or default constructor is Parameterized constructor. 

We can create parameterized constructor as shown below. 

class Employee 

{ 

       public Employee(int id, string name) 

       { 

       } 

} 

We can call the above Employee() parameterized constructor by passing the required parameters id and name while creating the object as shown below.  

class Program 

{ 

          static void Main(string[] args) 

          { 

                 Employee obj = newEmployee(100,"John");  

          }

} 

We can use C# class constructor to initialize the values for the variables before calling any method in the class.