What is Constructor?
A special method of the class that is automatically invoked when an instance of the class is created is called a constructor. A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created.
When we create instance of a class a default constructor is invoked automatically having the same name as Class . Some key points,
- Constructor is used to initialize an object (instance) of a class.
- Constructor is a like a method without any return type.
- Constructor has the same name as class name.
- Constructor follows the access scope (Can be private, protected, public, Internal and external).
- Constructor can be overloaded.
Following is the list of constructors in C#.
- Default constructor
- Parameterized constructor
- Private Constructor
- Static Constructor
- Copy Constructor
Default Constructor
A constructor without any parameters is called a default constructor. If we do not create constructor the class will automatically call default constructor when an object is created.
Example of Default Constructor,
- using System;
- namespace DefaultConstractor {
- class addition {
- int a, b;
- public addition() //default contructor
- {
- a = 100;
- b = 175;
- }
- public static void Main() {
- addition obj = new addition(); //an object is created , constructor is called
- Console.WriteLine(obj.a);
- Console.WriteLine(obj.b);
- Console.Read();
- }
- }
- }

Arkadeep DePosted Jan 9, 2021, 7:10 PM
Nice one, especially the header.
Saravanakumar SekaranPosted Jan 7, 2021, 8:10 AM
Very Nice Article !!!