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:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BackToBasics
  6. {
  7. class DefaultConstr
  8. {
  9. public string val1, val2;
  10. public DefaultConstr()//Default Constructor
  11. {
  12. //Console.WriteLine("Default Constructor");
  13. val1 = "iShriss";
  14. val2 = "Sharma";
  15. }
  16. }
  17. class ParameterizedConstr
  18. {
  19. public string para1, para2;
  20. public ParameterizedConstr(string name, string surname)//Parameterized constructor
  21. {
  22. para1 = name;
  23. para2 = surname;
  24. }
  25. }
  26. class StaticConstr
  27. {
  28. public string value1, value2;
  29. static StaticConstr() //Static constructor
  30. {
  31. //initialize field that needs to be execute only one
  32. Console.WriteLine("This is Static constructor.Static constr invoked only once.!");
  33. }
  34. public StaticConstr()
  35. {
  36. value1 = "Instance constructor invoked everytime when object is created.";
  37. value2 = "This is instance(default) constructor";
  38. }
  39. }
  40. class PrivateConstr
  41. {
  42. public string fname, lname;
  43. public PrivateConstr(string Firstname, string Lastname)
  44. {
  45. fname = Firstname;
  46. lname = Lastname;
  47. }
  48. private PrivateConstr()//private constructor
  49. {
  50. Console.WriteLine("This is private constr which retrict instantiation.");
  51. }
  52. }
  53. class Program
  54. {
  55. static void Main(string[] args)
  56. {
  57. DefaultConstr obj = new DefaultConstr(); // Default constructor
  58. Console.WriteLine("Default Constructor:"+Environment.NewLine);
  59. Console.WriteLine(obj.val1);// Output:iShriss
  60. Console.WriteLine(obj.val2); //Output:Sharma
  61. Console.WriteLine(Environment.NewLine);
  62. Console.WriteLine("Parameterized Constructor:"+Environment.NewLine);
  63. ParameterizedConstr objParameter = new ParameterizedConstr("Shridhar", "Sharma"); //Parameterized Constructor
  64. Console.WriteLine(objParameter.para1+","+objParameter.para2);//Output:Shridhar,Sharma
  65. Console.WriteLine(Environment.NewLine);
  66. Console.WriteLine("Static Constructor:"+Environment.NewLine);
  67. // At this time both constructors(Static constr(invoke once) and instance(default) constructor invoked)
  68. StaticConstr objStatic = new StaticConstr();//Static and Instance constructor invoked.
  69. Console.WriteLine("Both constructors will invoked."+Environment.NewLine);
  70. Console.WriteLine(objStatic.value1);
  71. Console.WriteLine(objStatic.value2);
  72. Console.WriteLine(Environment.NewLine);
  73. Console.WriteLine("Private Constructor:"+Environment.NewLine);
  74. //PrivateConstr objPrivateCtr = new PrivateConstr();//Not allowed because of private cnstr
  75. PrivateConstr objPrivate = new PrivateConstr("iShriss", "Sharma"); //will execute
  76. Console.WriteLine(objPrivate.fname + "," + objPrivate.lname);//Output:Shridhar,Sharma
  77. Console.ReadKey();
  78. }
  79. }
  80. }
Output



Closure

By going through this article we understood the basic definition and implementation of various types of constructors.