0

What is Constructor and its type in c#

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

Remarks on Constructor:-

1. Constructor is called automatically when we create object of class.

  1. Name of constructor should be same as name of class.
  2. Constructor does not return any value.
  3. Constructor should have 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 constructor.

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 value to the private properties of the class. This constructor will automatically call when we create an object of this class.

 

 

How Default constructor will be 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 developer ,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 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.

Admin

Leave a Reply

Your email address will not be published. Required fields are marked *