What is constructors and it categories in C#

Constructors

Before discuss about it first we know what is constructor.

Constructor is a special type of method that enables an object to initialize itself when it is created. It has the same name as the class itself. They do not specify a return type, not even void. This is because they do not return any value. Constructor is usually public because they are provided to create objects. It can also be declared as private or protected. In such case objects of that class cannot be created and also the class cannot be used as a base class for inheritance.

Example of constructor

  1. Class Rectangle  
  2.   
  3. {  
  4.   
  5.     Public int length;  
  6.   
  7.     Public int width;  
  8.   
  9.     Public Rectangle(int x, int y) // constructor method  
  10.   
  11.     {  
  12.   
  13.         Length = x;  
  14.   
  15.         Width = y;  
  16.   
  17.     }  
  18.   
  19.     Public int RectArea()  
  20.   
  21.     {  
  22.   
  23.         Return(length * width);  
  24.   
  25.     }  
  26.   
  27. }  
Overload Constructors

Overload constructor follow the same concept of method overloading in c#. Method overloading means same name but different parameter lists and different definitions. To create an overloading constructor method we have to do several different constructor definitions with different parameter lists. The difference may be in either the number or type of arguments.

Example of overloaded construstors

  1. Class Room  
  2.   
  3. {  
  4.   
  5.     Public double length;  
  6.   
  7.     Public double breadth;  
  8.   
  9.     Public Room(double x, double y) // first one  
  10.   
  11.     {  
  12.   
  13.         Length = x;  
  14.   
  15.         Breadth = y;  
  16.   
  17.     }  
  18.   
  19.     Public Room(double x) // second one  
  20.   
  21.     {  
  22.   
  23.         Length = breadth = x;  
  24.   
  25.     }  
  26.   
  27. }  

Types of constructors

Basically 5 types of constructors available.

These are

  1. Default Constructor.
  2. Parameterized constructor.
  3. Copy constructor(c sharp doesn’t support it)
  4. Static constructor.
  5. Private constructor.

Default Constructor

It is a constructor type created without parameter. If we do not create any constructor in a class it created automatically.

Parameterized constructor

A constructor having one or more parameters is called parameterized constructor.

Copy constructor

A copy constructor creates an object by copying variables from another object. This type of constructor c# doesn’t support. We will not explain much more about it here.

Static constructor

Static constructor is declared by prefixing a static keyword to the constructor definition. It cannot have any parameter. No access modifier on static constructors. A class can have only one static constructor.

Private constructor

It is a constructor that declared with private access modifier in a class.We cannot create an object which class contain private constructor.Which class contain private constructor we cannot inherited that class. To access the methods or properties in a class with private constructor we can assign methods or properties with static keyword.

Some important points about private constructor

  1. We can use it when we have only static member.
  2. It use for implementation of singleton class pattern.
  3. If we want to create object of the class which contain private constructor we can have a set of public constructor along with the private constructor.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now