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:
- 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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace BackToBasics
- {
- class DefaultConstr
- {
- public string val1, val2;
- public DefaultConstr()
- {
-
- val1 = "iShriss";
- val2 = "Sharma";
- }
- }
-
- class ParameterizedConstr
- {
- public string para1, para2;
- public ParameterizedConstr(string name, string surname)
- {
- para1 = name;
- para2 = surname;
- }
- }
-
- class StaticConstr
- {
- public string value1, value2;
-
- static StaticConstr()
- {
-
- 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()
- {
- Console.WriteLine("This is private constr which retrict instantiation.");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
-
- DefaultConstr obj = new DefaultConstr();
-
- Console.WriteLine("Default Constructor:"+Environment.NewLine);
- Console.WriteLine(obj.val1);
- Console.WriteLine(obj.val2);
- Console.WriteLine(Environment.NewLine);
-
- Console.WriteLine("Parameterized Constructor:"+Environment.NewLine);
-
- ParameterizedConstr objParameter = new ParameterizedConstr("Shridhar", "Sharma");
- Console.WriteLine(objParameter.para1+","+objParameter.para2);
- Console.WriteLine(Environment.NewLine);
-
- Console.WriteLine("Static Constructor:"+Environment.NewLine);
-
-
- StaticConstr objStatic = new StaticConstr();
- 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 objPrivate = new PrivateConstr("iShriss", "Sharma");
- Console.WriteLine(objPrivate.fname + "," + objPrivate.lname);
- Console.ReadKey();
- }
- }
-
- }
Output
Closure
By going through this article we understood the basic definition and implementation of various types of constructors.