Types of Constructor

This is a n issue of debate but I always like to segregated Constructors in following types:

Default Constructor

A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.

Parameterized Constructor

Besides a default constructor when its need to pass some argument on the initialization of a class a parameterized constructor is come to picture. It follows the same rules as default constructor with a difference that it has some parameters which also differentiate it from default constructor. Go through following snippet:

Note:1. A default constructor should be explicitly declared while declaring parameterized constructor.

2. Some writer also take Private constructor and Static Constructor as types of constructor but in my view these are constructor with different modifiers so behave differ; I will cover these in next section.

Access Modifiers and Prefix with Constructors

By default Constructors are public but we can also use other modifiers and prefix like private and static. With the use of these modifiers constructors behave differently:

Using Access Modifier private with Constructor

When put private as access modifier to constructor then that constructor is known as private Constructor

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.

Some time there is a need where we have not allow outer world to get instantiate our class then we need to use private modifier with constructor. Consider following piece of code:

it throws an error: Constructors. MsDotNetHeaven. MsDotNetHeaven ()' is inaccessible due to its protection level

Now, question comes to mind if the above contains error then what the use is of private constructor, we can make above class to be initialized by declaring another public constructor, modify above code-snippet with bellow one:

Using prefix static with Constructor

With the use of static with constructor gives a name as static constructor

For C++ developers it’s a new concept introduced in C#.

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.Consider the following:

(A) From above : the sequence of invoking a constructor is first public MsDotNetHeaven() and then public MsDotNetMentor() (B) From above : the sequence of invoking a constructor is public MsDotNetHeaven(String strName)and then public MsDotNetMentor(String strName)

Note:

A static constructor should not be declared with any access modifier.

A static constructor does not accept parameters

A static constructor is called automatically.

There is no way to call a static constructor directly.

Can’t stop the execution of Static constructor

Due to the scope of this article it might be possible that something I forgot please remind me if any :

Important :

Constructor is nothing but a special method, which initializes the class or its task to initialize the object of it class.

Its name must be same as the name of class

This is a special method as constructors do not have return types, not even void

Constructor cannot return any value because they didn’t have any return type.

Constructor can’t be get inherited, although a derived class can class the base class constructor.

A class has atleast one constructor also known as default constructor [a constructor without parameter]

You have to explicitly write a default constructor while Overloading constructors.

Concept declaring multiple constructors of a class with different sets of parameters known as Constructor overloading.