Constructor in Console Application

In previous .net post we have already learned about create console application and namespace in console application c# .net.

In this console application c# tutorials we will understand what is constructor, types of constructor and features of constructor in detail.
Before creating a c# example, we should have a clear idea about constructor and class.

What is constructor in C# .net

Constructor is a special method or function of class having no return type and same name as class name.

– Constructor with no return type.
– Constructor name is same as class name.
– If we do not create constructor for class, the compiler will create a default constructor internally.
– The constructor with no argument and no body known as default constructor.
– Constructor with argument is known as parameterized constructor.
– All constructor by default public, we can also create private constructor.
– Static constructor automatically call at the time of class loading.
– We can only one static constructor can create in a class.

The class can have any number of constructor and constructor do not have return type. The constructor name must be same as class name.

here we create a constructor the constructor method name same as class name shows in below example.

Example of constructor in c#

 Types of constructor

– Default Constructor
– Parameterized Constructor
– Cpopy Constructor
– Static Constructor
– Private Constructor

1. Default Constructor

– A constructor without any parameters called default constructor. We can also say this type of constructor does not take any parameters.
Default constructor all instance of the class will be initialized without take any parameter value.

Example code of default constructor

The output of default constructor example is :


2. Parameterized Constructor

– A constructor with at least one parameter is called parameterized constructor. In this type of constructor we can initialize each instance of class to different values.

Example of parameterized constructor in c#

The output of parameterized constructor example is :

The sum  of 10 and 15 = 25

3. Copy Constructor

– A Parameterized constructor that contain a parameter of same class type is called copy constructor. Main use of this constructor is to initialize new instance to the values of an existing instance.

Example of copy constructor in c# .net

The output of copy constructor example is :

The sum of 10 and 15 = 25

4. Static Constructor

A static constructor has the same name as the class name preceded with the static keyword.
A static constructor will execute automatically whenever class of instance is created.
A static constructor will not have any access modifiers. Static constructor will not return anything.
Static constructor will call at the time of class loading. Static constructor will not allow overloading, there is no parameterized static constructor.

Example of static constructor in console application c#

The output of static constructor example is :

Example of Static Constructor
Example of Instance Constructor = 10
Example of Instance Constructor = 10

5. Private Constructor

We can also create a constructor as private. If a class contain at least one private constructor, then it is not possible to create an instance for this class.
Private constructor is used in a class that contain static member only.Private constructor is used to restrict the class from being instantiated when it contain every member as static.

Example of Private Constructor in console application c#

The output of private constructor example is :

The sum of 10 and 15 = 25

I hope you will understand what is constructor and type of constructor in this post.

Leave a Reply

Your email address will not be published. Required fields are marked *