Constructors in C-Sharp

Human Digital
Nerd For Tech
Published in
5 min readMar 15, 2021

--

To develop any application, defining the variables forms a very critical part of the process. In the .Net framework, the definition of simple data types to their default values is done by C#. However, while creating specific classes or struct, one would have to decide default values which can be achieved by using constructors.

Constructors are typically utilized for initializing the attributes of a class. It also allocates memory for the class while initializing the data members of a class.

In this post, we are going to understand what are constructors in C# and how you can define the different types of constructors in it.

Table of content

● What is a constructor?

● Syntax of a constructor

● Salient Features of constructors

● What are the types of constructors?

● Conclusion

What is a constructor in C#?

Constructors are the methods that are invoked when an instance of a class or struct is created. Constructors are generally used to initialize the private fields in the class or struct when an instance of the class is created. A default constructor is created for a class when no constructor is explicitly defined by the programmer. So, each class will always have at least one constructor even if none is created by the programmer. This default constructor would initialize all the numeric data values to zero and objects and string to null.

Syntax of a constructor

Following is the general syntax of a constructor:-

Above is the general syntax of constructor the access specifier various according to the need of the program.

Example:-

In the above image, you can see that Fruits is the class in which a constructor with the same name as the name of the class is created. The constructor is public, so there is no limitation to its access. Hence when objects of the class are created, the data members of this constructor will also be accessible in the child classes.

Salient features of constructors

Key features of constructors are:

  • Any number of constructors can be there for a class.
  • It doesn’t have a return type. This includes even void.
  • The constructor of a static class can never be parameterized.
  • Only one static constructor can be created within a class.
  • Constructors can be overridden.

What are the types of Constructors?

Default Constructor

When no parameter is passed to the constructor, the constructor is said to be a default constructor. The major drawback of a default constructor is that we cannot initialize the different instances of classes with different values as all the instances will be created using the same values that are defined in the default constructor. This default constructor would initialize all the numeric data values to zero and objects and string to null.

Following is the example of default constructor -

**

Output:

Parameterized Constructor

When a constructor takes at least one parameter, then it is called a parameterized constructor. The advantage of parameterized constructor over a default constructor is that we can initialize different instances of a class with different values making it more dynamic and robust.

Following is the example of parameterized constructor:-

Output:-

Copy Constructor

A copy constructor is a constructor that copies the variable from another object while creating a new object. The copy constructor is mainly used to initialize a new instance with values of a previous instance.

Syntax of copy constructor is as follows:-

In the above image, a copy constructor is created using an existing instance. The copy constructor copies all data members of this existing instance.

Static constructors

When a constructor is defined using the static keyword, then it gets invoked only once for all the instances i.e., when the first instance of the class is created. The use of static constructor in C# is to initialize the static fields of the application which need to be executed only once for all instances.

Key features of static constructors:-

  • No access specifiers are given to the static constructors nor does it have any parameters.
  • A static constructor is called only when the first instance of the class is created.
  • A static constructor cannot be directly called.
  • The execution of a static constructor in a program cannot be controlled by the user.

Syntax of a static constructor is as follows:-

**

In the above image, we can see that a static keyword is used to create a static constructor. Inside a static constructor, there can only be data members that are to be initialized only once during the execution of the program.

Private Constructor

When a constructor is created with a private access specifier it is called a private constructor This type of constructor can only be used when all the members of a class are static.

Syntax of a private constructor is as follows:-

**

In the above image, we can see that a private keyword is used to create the private constructor. Any instance cannot call the private constructor. The private constructor only contains static data members.

Conclusion

This post has covered some of the key concepts of constructors along with its uses. In your next C# application, use constructors whenever you are defining private fields in the class or struct when an instance of the class is created.

Originally published at https://www.partech.nl.

--

--

Human Digital
Nerd For Tech

The people first agency. Digitale oplossingen, professionals en trainingen waarin de mens altijd voorop staat | humandigital.nl