Constructor is a special method which automatically invokes at the time of instantiation. It is a special method because it do not have a return type and named as name of class.
Constructor initializes the fields of the class and members of new object. A class has at least one constructor. If there is no constructor in the class, then the compiler will automatically invoke default constructor.
Constructor without parameter is known as default constructor.
Types of Constructors:
Constructor initializes the fields of the class and members of new object. A class has at least one constructor. If there is no constructor in the class, then the compiler will automatically invoke default constructor.
Constructor without parameter is known as default constructor.
Types of Constructors:
- Default Constructor
- Parameterized Constructor
- Static Constructor
- Private Constructor
Default Constructor
Constructor without parameter is known as default constructor. It initializes all numeric fields in a class to zero and all string and object fields to null.
Parameterized Constructor
Constructor with at least one parameter is known as Parameterized constructor, so that it can initialize each instance of the class to different values.
Static Constructor
Static constructor is used to initialize static fields of the class, in static field we write the code that is required to execute only once because static constructor gets invoked only once. It is called at the time of creation of first instance of class by CLR.
Private Constructor
Private constructor is generally used in class with static members only. If a class has only private constructor, other classes cannot create instance of this class.
Code Snippet:
Constructor without parameter is known as default constructor. It initializes all numeric fields in a class to zero and all string and object fields to null.
Parameterized Constructor
Constructor with at least one parameter is known as Parameterized constructor, so that it can initialize each instance of the class to different values.
Static Constructor
Static constructor is used to initialize static fields of the class, in static field we write the code that is required to execute only once because static constructor gets invoked only once. It is called at the time of creation of first instance of class by CLR.
Private Constructor
Private constructor is generally used in class with static members only. If a class has only private constructor, other classes cannot create instance of this class.
Code Snippet:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace BackToBasics
- {
- class DefaultConstr
- {
- public string val1, val2;
- public DefaultConstr()//Default Constructor
- {
- //Console.WriteLine("Default Constructor");
- val1 = "iShriss";
- val2 = "Sharma";
- }
- }
- class ParameterizedConstr
- {
- public string para1, para2;
- public ParameterizedConstr(string name, string surname)//Parameterized constructor
- {
- para1 = name;
- para2 = surname;
- }
- }
- class StaticConstr
- {
- public string value1, value2;
- static StaticConstr() //Static constructor
- {
- //initialize field that needs to be execute only one
- Console.WriteLine("This is Static constructor.Static constr invoked only once.!");
- }
- public StaticConstr()
- {
- value1 = "Instance constructor invoked everytime when object is created.";
- value2 = "This is instance(default) constructor";
- }
- }
- class PrivateConstr
- {
- public string fname, lname;
- public PrivateConstr(string Firstname, string Lastname)
- {
- fname = Firstname;
- lname = Lastname;
- }
- private PrivateConstr()//private constructor
- {
- Console.WriteLine("This is private constr which retrict instantiation.");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- DefaultConstr obj = new DefaultConstr(); // Default constructor
- Console.WriteLine("Default Constructor:"+Environment.NewLine);
- Console.WriteLine(obj.val1);// Output:iShriss
- Console.WriteLine(obj.val2); //Output:Sharma
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("Parameterized Constructor:"+Environment.NewLine);
- ParameterizedConstr objParameter = new ParameterizedConstr("Shridhar", "Sharma"); //Parameterized Constructor
- Console.WriteLine(objParameter.para1+","+objParameter.para2);//Output:Shridhar,Sharma
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("Static Constructor:"+Environment.NewLine);
- // At this time both constructors(Static constr(invoke once) and instance(default) constructor invoked)
- StaticConstr objStatic = new StaticConstr();//Static and Instance constructor invoked.
- Console.WriteLine("Both constructors will invoked."+Environment.NewLine);
- Console.WriteLine(objStatic.value1);
- Console.WriteLine(objStatic.value2);
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("Private Constructor:"+Environment.NewLine);
- //PrivateConstr objPrivateCtr = new PrivateConstr();//Not allowed because of private cnstr
- PrivateConstr objPrivate = new PrivateConstr("iShriss", "Sharma"); //will execute
- Console.WriteLine(objPrivate.fname + "," + objPrivate.lname);//Output:Shridhar,Sharma
- Console.ReadKey();
- }
- }
- }
Output
Closure
By going through this article we understood the basic definition and implementation of various types of constructors.

Shakti SaxenaPosted Sep 11, 2015, 9:00 AM
Nice article
Atul RawatPosted Sep 4, 2015, 9:15 AM
nice Article sir
Rajeesh MenothPosted Sep 4, 2015, 12:53 AM
Nice Share
Mohammed IbrahimPosted Sep 3, 2015, 11:36 AM
thanks for sharing information...
Shridhar SharmaPosted Sep 3, 2015, 11:32 AM
Thanks all.
Pankaj Kumar ChoudharyPosted Sep 3, 2015, 8:14 AM
Short and Nice Article.........
Sibeesh VenuPosted Sep 3, 2015, 4:55 AM
Nice
Humayun Kabir MamunPosted Sep 3, 2015, 1:32 AM
Nice...
Yashwanth MuthineniPosted Sep 3, 2015, 1:09 AM
Nice Share..