What Is Constructor And What Are Its Types?

A constructor is a special type of function with no return type. Name of constructor should be same as the name of the class. We define a method inside the class and constructor is also defined inside a class. A constructor is called automatically when we create an object of a class. We can’t call a constructor explicitly. Let us see the types of constructor.

Remarks on Constructor

  1. Constructor is called automatically when we create an object of the class.
  2. Name of constructor should be same as the name of the class.
  3. Constructor does not return any value.
  4. Constructor should have a public access modifier.

Constructor Types 

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor 

In this article, I am going to explain only two types of constructors.

Default  Constructor

Default constructor does not take any parameter. C# compiler creates a default constructor if we do not define any default constructor inside the class, We can also define the default constructor by writing some codes. I defined a default constructor to assign properties of a class. Let us see with the help of an example.

public  class Adminclass  
{  
    string userId = string.Empty;  
    string password = string.Empty;  
   //Default constructor, having no any parameter  
   public Adminclass()  
   {  
       userId = "shr";  
       password = "Pass";   
   }  
}

In the above example, I defined one class named as Adminclass and one default constructor with the same name as the name of the class. I used default constructor to assign the value to the private properties of the class. This constructor will automatically call when we create an object of this class.

How Default constructor is called.

Adminclass adminObject = new Adminclass();  

Remarks on Default constructor

  1.  Default constructor is created by the compiler if we do not create any constructor inside the class.
  2.  Default constructor does not take any parameter.
  3.  Default constructor is called when we create an object of the class.

Parameterized Constructor

Parameterized Constructor is created by the developer. This constructor takes at least one parameter. I have defined one Parameterized constructor below.

public  class Adminclass  
  
   string userId = string.Empty;  
   string password = string.Empty;  
  //Parameterrized constructor, having parameters  
  public Adminclass(string username,string userPassword)  
  {  
      userId = username;  
      password = userPassword;  
  }  
}

In the above example, I defined one class named as Adminclass and one parameterized constructor with the same name as the name of the class. This constructor takes two parameters as you can see in the above example.  I used a parameterized constructor to assign values to the private properties of the class. This constructor will automatically call when we create an object of this class. Let us see how we can call this Parameterized constructor.

How Parameterized constructor will be called.

Adminclass adminObject = new Adminclass(“Shreesh”,”mypassword”);

Remarks on Default constructor:

  1. Parameterized constructor is created by the developer, a compiler does not create any parameterized constructor.
  2. Parameterized constructor takes at least one parameter.
  3. Parameterized constructor is called when we create an object of the class.

Remark

If we define any constructor, then the C# Compiler does not define a default constructor. A default constructor is created by the compiler when we do not create any constructor inside the class.