Introduction :

In this blog we will see the primary constructors in c#.

What is a  constructor in C#?

A constructor is a particular class method that is invoked whenever a new instance of the class is created. A constructor, like a method, contains a set of instructions that are executed when an object is created. It is used to initialize the values for data members of the same class.

Types of Constructors in C#:

There are 5 Types of Constructors in C#, they are:

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

Default Constructor :

A “default constructor” is a constructor that takes no input arguments. In a default constructor, each class instance must be initialized with the same data. Within a class, the default constructor sets all numeric fields to zero and all string and object fields to null.

Parameterized Constructor:

A parameterized constructor is a constructor that has at least one argument. It has the ability to set different values for each instance of the class.

Copy  Constructor:

This constructor creates an object by copying variables from another object. Its principal function is to set a new instance’s values to those of an existing one.

Private Constructor:

A private constructor is one that was created using the private specifier. Other classes cannot inherit from this class, and no instance of this class can be created.

 

Static Constructor:

This constructor is invoked only once in the class, when the first reference to a static member of the class is made, which is when the static constructor is invoked. A static constructor is used only once to initialise the class’s static fields or data.

For any Help or Queries Contact us on info@crmonce.com or +918096556344