What is a constructor in C#?
A special method of the class that is automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize the private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor of the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
Some of the key points regarding constructor are
- A class can have any number of constructors.
- A constructor doesn't have any return type, not even void.
- A static constructor can not be a parametrized constructor.
- Within a class, you can create one static constructor only.
In C#, constructors can be divided into 5 types
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
Now, let's see each constructor type with the example below.
Default Constructor in C#
A constructor without any parameters is called a default constructor; in other words, this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class with different values. The default constructor initializes:
- All numeric fields in the class to zero.
- All string and object fields to null.
Example
Now run the application, the output will be as following:
![default.png]()
Parameterized Constructor in C#
A constructor with at least one parameter is called a parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.
Now run the application, the output will be as following:
Copy Constructor in C#
The constructor which creates an object by copying variables from another object is called a copy constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing instance.
Syntax
The copy constructor is invoked by instantiating an object of type employee and bypassing it the object to be copied. Example
Now, emp1 is a copy of emp2.
Let's see its practical implementation.
Now run the program, the output will be as follows:
![Copyconstrctor.png]()
Static Constructor in C#
When a constructor is created using a static keyword, it will be invoked only once for all of the instances of the class and it is invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.
Some key points of a static constructor are:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control over when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Syntax
Now let's see it practically.
Now run the program the output will look as in the following:
![Static.png]()
Private Constructor in C#
When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class. They are usually used in classes that contain static members only. Some key points of a private constructor are:
- One use of a private constructor is when we have only static members.
- It provides an implementation of a singleton class pattern
- Once we provide a constructor that is either private or public or any, the compiler will not add the parameter-less public constructor to the class.
Now let's see it practically.
Now run the application. The output is as follows.
![Private.png]()
If you uncomment the preceding statement that is commented in the above program then it will generate an error because the constructor is inaccessible (private).
Summary
In this article, I discussed various types of constructors in C# language, I hope you found this article useful