Monday, November 1, 2021

C# Constructor and its types


Constructor and its types

What is a constructor? The C# is an object-oriented programming (OOP) language in which object is the fundamental programming unit.  When an object is to be created, a template is required for it, which is called class in the language of object-oriented programming. There are two parts in a class, one part is in the form of fields to represent the data of the object while other parts are methods in the class which are used to work upon the data of the class. The methods of a class outline the behavior of the class.

When an object of a class is created, the new operator is used to create it in the C#. Also, during the creation of the object, the fields of the class related to that object are initialized.  Initialization refers to assigning initial value to the fields of the class.  

Constructor is a device with the help of which the fields of the class are initialized. Constructor literally means to construct, to create. Constructor is helpful in creating an object. An object cannot be created without assigning initial values to the fields. Constructor helps to create an object by assigning initial values ​​to the fields of a class.

Constructor is a special type of method of a class that initializes the fields of the class but it does not return anything. The reason is that its main purpose is simply to initialize the fields of the class. So the constructor does not return anything.

Conventionally, the name of constructor is always the name of the class and in C#, it is always defined inside the class itself.  Constructor also has scope which is expressed with the help of modifier such as public, private, protected etc.

Every class must have a constructor. Why?
This is because the object cannot be allocated memory without initializing the fields.  The size of the memory of an object within the heap is determined according to the data type of the different fields.  When an object within the heap takes up memory, the initial value of each field of that object is also determined. The size of these fields in the heap in totality determines the memory required for the object.

Implicit constructor
The constructor which is not visible in the class is called Implicit constructor.  This happens when no constructor in the class is given by the programmer.  The Implicit constructor is also called default constructor because the Implicit constructor initializes all the fields of the class with their default values, corresponding to the data type of those fields.  For example, if the field is of type int, the default constructor will give the initial value of that field zero. Similarly, if the field is of type string then the default constructor of the field will give null value and if a field is of bool type then the default constructor will give its default value of false.

Following are some of the features of default constructor:
  • The default constructor is always public.
  • The default constructor is always parameterless.
  • The default constructor is not visible within the class. It's implicit.
  • The compiler executes the default constructor when an object is created using the new operator.
  • The default constructor is not available when any constructor is defined by the programmer. The default constructor is availed by compiler if no constructor is defined by the programmer.
Although the default constructor is defined by the compiler, its execution has to be done explicitly by the programmer.  Remember that any non static constructor has to be called explicitly.
Explicit Constructor
When a constructor is defined by the programmer then it is called Explicit Constructor.  The Explicit constructor can be a parameter less constructor or it can also be a parameterized constructor. When the constructor is an explicit constructor, the constructor can initialize the class's fields to their appropriate values.  In such a case there is no need to initialize the fields with their default values.

The significant feature of the Explicit constructor is that we can initialize the fields of different objects with different values.  In this way it becomes possible to create different objects of the same class.  for example, look at the following code:

Implicit constructor is always public but nothing like that is required with explicit constructor.  Explicit constructor can also be private constructor.  Also, any other modifier is applicable for explicit constructor.

Private Constructor
The object of a class can be created by private constructor only within the class within which the private constructor is defined.  When another class wants to create an object of that class, it will not be possible.  In other words, a class with a private constructor cannot be consumed by any other class.  Conversely, a class with a public constructor can be consumed by any other class.

Protected Constructor
A class with a protected constructor can be consumed by that class or by any number of child classes of that class.

Parameterized Constructor
The advantage of parameterized constructors is that when the user creates an object of this class, anyone can create the object by passing the containing values ​​to the fields.  But if user gives wrong argument of parameter to value of fields i.e. does not use proper data type then object cannot be created.

It becomes clear from the above description so far that whatever the type of constructor is, the function of the constructor is to provide the initial value to the data of a class.  Every class must have an explicit or implicit constructor.  If there is a creation, then there should be a creator. Similarly, an object must have a constructor.

Differences between static constructor and non-static constructor

 1) A class has a static constructor only if the class contains a static field.  If the class does not have a static field then it will not have a static constructor.  The static constructor is used to initialize the static field of the class.

 2) The static constructor is not called explicitly.  The static constructor is automatically called at the beginning of the class's lifecycle.  The life cycle of a class refers to the different code blocks within the class that are run.  When a class is executed, its constructor is first called and if there is a static field within that class then the static constructor gets called automatically.  Remember that non static constructor is always explicitly called by programmer whereas static construction is implicitly called by compiler.

 3) The static constructor can be implicitly or explicitly defined.  When a static constructor is implicitly defined, the static field is initialized by the compiler to its default value. Contrary to this, When the static constructor is explicitly defined, the static field is initialized by the programmer to an appropriate value.

 4) The static constructor is always parameterless.  One reason behind this is that the static constructor is called at the very beginning of the life cycle of the class and at that time it is not possible to pass any parameter values ​​to the static constructor.

 5) As the static constructor is parameterless, it is not possible to overload the static constructor as overloading requires a method or constructor to have a parameter.

 6) Static constructor is always called implicitly whereas non static constructor is always called explicitly.

 7) If class has static constructor and non-static constructor then static constructor is always called before non-static constructor.

 8) Static constructor is called only once in the entire life cycle of the class whereas non-static constructor can be called as many times as desired in the life cycle of the class.  This is because the static constructor is called by the compiler whereas the non-static constructor is called by the programmer.

 9) Static constructor is called automatically at the beginning of the entire life cycle of the class whereas non-static constructor can be called at any stage in the entire life cycle of the class.

10) Static constructor is not given modifier.

Copy constructor
The copy constructor is a non-static parameterized constructor in which the object of the same constructor class is passed as a parameter.


Edited on 5th July 2023

No comments:

Post a Comment

Hot Topics