Constructor And Its Types In C#

In this article, I am going to share with you about Constructor and its types in C#
 
What is Constructor?
  1. A constructor is a special method of a class, that is called automatically when an object/instance of the class is created.
  2. Constructor name is always the same as the class name. If a class name is 'A', then the constructor of this class is 'A()'.
  3. A class can contain any number of constructors, but with different arguments.
  4. If the constructor of a class is not declared, then the compiler will automatically create a default constructor for that class. The default constructor will initialize all the numeric (int, double) fields with '0', bool fields with 'false', and the object and reference fields with 'null'.
  5. A constructor does not have any return type (any data type like int, bool, string, void etc).
  6. A constructor is useful in setting the default values in order to initialize data members of the class when an object of the class is created.
  7. A class can contain only one static constructor.
  8. The constructor can be of five types. The syntax of each type is a bit different.
  9. We use 'base' keyword to call the constructor of a base class when inherited.
Constructors can be of 5 types
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor
Default Constructor
  • Default Constructor is a constructor without parameters. This means it does not take any parameter.
  • A Default Constructor is automatically called whenever an instance or object of the class is created with 'new' operator and no arguments on it.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. This will call the default constructor automatically  
  10.             Operations Operations = new Operations();  
  11.   
  12.             Operations.Addition(33, 33);  
  13.   
  14.             // Another Instance of Operations class. Everytime an instance of operations is created, default constructor will be             called automatically  
  15.             Operations operations2 = new Operations();  
  16.             operations2.Addition(133, 33);  
  17.             operations2.Subtraction(100, 5);  
  18.   
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22.   
  23.     class Operations  
  24.     {  
  25.         public int Num1;  
  26.         public int Num2;  
  27.         public double result;  
  28.         public string str;  
  29.   
  30.         // Default Constructor. Constructor name is always same with class name  
  31.         public Operations()  
  32.         {  
  33.   
  34.             Num1 = 100;  
  35.             Num2 = 200;  
  36.             str = "Hello Buddy";  
  37.         }  
  38.   
  39.         //Method  
  40.         public double Addition(int a, int b)  
  41.         {  
  42.             result = a + b;  
  43.             Console.WriteLine("Result of Addition = " + result);  
  44.             return result;  
  45.         }  
  46.   
  47.         //Method  
  48.         public double Subtraction(int a, int b)  
  49.         {  
  50.             result = a - b;  
  51.             Console.WriteLine("Result of Subtraction = " + result);  
  52.             return result;  
  53.         }  
  54.   
  55.     }  
  56. }  
Output
  1. Result of Addition = 66  
  2. Result of Addition = 166  
  3. Result of Subtraction = 95  
In the above program, we create a class 'Operations' that consists of its default constructor. Inside constructor, we define variables 'Num1', 'Num2' and 'str' with values '100', '200' and 'Hello Buddy' respectively. Whenever an instance of 'Operations' class is created, the constructor will be called automatically which will assign the value of 'Num1', 'Num2', and 'str'.
  • If the constructor of a class is not declared, then the compiler will automatically create a default constructor for that class. The default constructor will initialize all the numeric (int, double) fields with '0', bool fields with 'false', and the object and reference fields with 'null'.
  • When we create a default constructor of the class without access modifier, then it is by default private. While when we do not create any constructor, then the compiler will automatically create a default constructor with access modifier as public.
  • Every object/instance of the class with default constructor will be initialized with the same values.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. This will call the default constructor automatically  
  10.             Operations Operations = new Operations();  
  11.   
  12.             Operations.Display();  
  13.   
  14.             // Another Instance of Operations class. Everytime an instance of operations is created, default constructor will be             called automatically  
  15.             Operations operations2 = new Operations();  
  16.             operations2.Display();  
  17.             operations2.Subtraction(100, 5);  
  18.   
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22.   
  23.     class Operations  
  24.     {  
  25.         public int Num1;  
  26.         public int Num2;  
  27.         public double result;  
  28.         public string str;  
  29.   
  30.         public void Display()  
  31.         {  
  32.             Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);  
  33.         }  
  34.   
  35.         public double Addition(int a, int b)  
  36.         {  
  37.             result = a + b;  
  38.             Console.WriteLine("Result of Addition = " + result);  
  39.             return result;  
  40.         }  
  41.   
  42.         public double Subtraction(int a, int b)  
  43.         {  
  44.             result = a - b;  
  45.             Console.WriteLine("Result of Subtraction = " + result);  
  46.             return result;  
  47.         }  
  48.   
  49.     }  
  50. }  
Output
  1. Value of Num1 = 0 ,Num2 = 0 ,Result = 0 and string =  
  2. Value of Num1 = 0 ,Num2 = 0 ,Result = 0 and string =  
  3. Result of Subtraction = 95  
In the program shown above, we have not created any constructor; so the compiler itself will create the default constructor. Default Constructor will assign all the numeric variables to '0' and string types to null if their values are not defined. So, the values of Num1, Num2 and result will be '0' and of str will be 'null'.
 
Parameterized Constructor
  • When we add parameters to the default constructor, it is said to be a parameterized constructor. This means a constructor with parameters is called the parameterized constructor.
  • A parameterized constructor must contain at least one parameter.
  • When we create a parameterized constructor of the class without access modifier, then it is by default private.
  • Every object/instance of the class with parameterized constructor can be initialized with different values.
  • A class can contain any number of parameterized constructors but with different datatypes or in numbers of the parameter.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. This will call the default constructor automatically  
  10.             Operations Operations = new Operations();  
  11.             Operations.Display();  
  12.   
  13.             // Another Instance of Operations class. This will call the parameterized constructor that has three parameters(two i               nt and one string)  
  14.             Operations operations1 = new Operations(22, 33, "heyyy");  
  15.             operations1.Display();  
  16.   
  17.             // Another Instance of Operations class. This will call the parameterized constructor that has two int parameters  
  18.             Operations operations2 = new Operations(4, 66);  
  19.             operations2.Display();  
  20.   
  21.             // Another Instance of Operations class. This will call the parameterized constructor that has one parameter  
  22.             Operations operations3 = new Operations(4);  
  23.             operations3.Display();  
  24.   
  25.             // Another Instance of Operations class. This will call the parameterized constructor that has one parameter  
  26.             Operations operations4 = new Operations(4, 77, 6);  
  27.             operations4.Display();  
  28.   
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
  32.   
  33.     class Operations  
  34.     {  
  35.         public int Num1;  
  36.         public int Num2;  
  37.         public double result;  
  38.         public string str;  
  39.   
  40.         // Default Constructor. Constructor name is always same with class name  
  41.         public Operations()  
  42.         {  
  43.   
  44.             Num1 = 100;  
  45.             Num2 = 200;  
  46.             str = "Hello Buddy";  
  47.         }  
  48.   
  49.         // Parametrized Constructor. It is Default Constructor will parameters.  
  50.         // Parametrized Constructor must contain atleast one parameter else it will become Default constructor  
  51.         public Operations(int x, int y, string welcome_str)  
  52.         {  
  53.   
  54.             Num1 = x;  
  55.             Num2 = y;  
  56.             str = welcome_str;  
  57.         }  
  58.   
  59.         // Second Parametrized Constructor with two int parameters  
  60.         public Operations(int x, int y)  
  61.         {  
  62.   
  63.             Num1 = x;  
  64.             Num2 = y;  
  65.             str = "Hello Buddy";  
  66.         }  
  67.   
  68.         // third Parametrized Constructor with one int parameters  
  69.         public Operations(int x)  
  70.         {  
  71.   
  72.             Num1 = x;  
  73.             Num2 = 500;  
  74.             str = "Hello Buddy";  
  75.         }  
  76.   
  77.         // Fourth Parametrized Constructor with three int parameters  
  78.         public Operations(int x, int y, int z)  
  79.         {  
  80.   
  81.             Num1 = x;  
  82.             Num2 = y;  
  83.             str = "Hello Buddy";  
  84.         }  
  85.   
  86.         public void Display()  
  87.         {  
  88.             Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);  
  89.         }  
  90.   
  91.         //Method  
  92.         public double Addition(int a, int b)  
  93.         {  
  94.             result = a + b;  
  95.             Console.WriteLine("Result of Addition = " + result);  
  96.             return result;  
  97.         }  
  98.   
  99.         //Method  
  100.         public double Subtraction(int a, int b)  
  101.         {  
  102.             result = a - b;  
  103.             Console.WriteLine("Result of Subtraction = " + result);  
  104.             return result;  
  105.         }  
  106.   
  107.     }  
  108. }  
Output
  1. Value of Num1 = 100 ,Num2 = 200 ,Result = 0 and string = Hello Buddy  
  2. Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = h the yyy  
  3. Value of Num1 = 4 ,Num2 = 66 ,Result = 0 and string = Hello Buddy  
  4. Value of Num1 = 4 ,Num2 = 500 ,Result = 0 and string = Hello Buddy  
  5. Value of Num1 = 4 ,Num2 = 77 ,Result = 0 and string = Hello Buddy  
In the above program, we have created multiple parameterized constructors along with the default constructor. When an instance of class is created with new keyword and passes parameter on it, then its corresponding constructor will be called. Like we did in program,

Operations operations1 = new Operations(22,33,"heyyy");

This will call the parameterized constructor that has three parameters (first two are int and the third one is a string). If no such constructor is present, then the compiler will generate an error. Also, the variables can contain different values as we are passing the values while creating an instance.

Copy Constructor
  • A copy constructor is a parameterized constructor with a parameter of its type. This means the copy constructor is a constructor which creates an object by copying values from another object.
  • It is very useful to initialize a new instance with the value of an existing instance.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. This will call the default constructor automatically  
  10.             Operations Operations = new Operations();  
  11.             Operations.Display();  
  12.   
  13.             // Another Instance of Operations class. This will call the parameterized constructor that has three parameters(two i               nt and one string)  
  14.             Operations operations1 = new Operations(22, 33, "heyyy");  
  15.             operations1.Display();  
  16.   
  17.   
  18.             // Another Instance of Operations class. This will call the Copy constructor  
  19.             Operations operations2 = new Operations(operations1);  
  20.             operations2.Display();  
  21.   
  22.             // Another Instance of Operations class. This will call the Copy constructor  
  23.             Operations operations3 = new Operations(Operations);  
  24.             operations3.Display();  
  25.   
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29.   
  30.     class Operations  
  31.     {  
  32.         public int Num1;  
  33.         public int Num2;  
  34.         public double result;  
  35.         public string str;  
  36.   
  37.         // Default Constructor. Constructor name is always same with class name  
  38.         public Operations()  
  39.         {  
  40.   
  41.             Num1 = 100;  
  42.             Num2 = 200;  
  43.             str = "Hello Buddy";  
  44.         }  
  45.   
  46.         // Parametrized Constructor. It is Default Constructor will parameters.  
  47.         // Parametrized Constructor must contain atleast one parameter else it will become Default constructor  
  48.         public Operations(int x, int y, string welcome_str)  
  49.         {  
  50.   
  51.             Num1 = x;  
  52.             Num2 = y;  
  53.             str = welcome_str;  
  54.         }  
  55.   
  56.         // Copy Constructor. It contain parameter of its type.  
  57.         public Operations(Operations ops)  
  58.         {  
  59.   
  60.             Num1 = ops.Num1;  
  61.             Num2 = ops.Num2;  
  62.             str = ops.str;  
  63.         }  
  64.   
  65.   
  66.         public void Display()  
  67.         {  
  68.             Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);  
  69.         }  
  70.   
  71.   
  72.     }  
  73. }  
Output
  1. Value of Num1 = 100 ,Num2 = 200 ,Result = 0 and string = Hello Buddy  
  2. Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = heyyy  
  3. Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = heyyy  
  4. Value of Num1 = 100 ,Num2 = 200 ,Result = 0 and string = Hello Buddy  
In the above program, we created a copy constructor along with parameterized and default constructors. copy constructor will create an object by copying values from another object.
 
Static Constructor
  • A static constructor is the default constructor declared with the "static" keyword.
  • There can be only one static constructor for a class.
  • A static constructor is called only once for all the instances of the class and is called during the initiation of the first instance.
  • If a class contains static and default constructor both then when the first instance of the class is initialized, both the constructors called one after another(static then default) and for every other instance default constructor will be called. Means for the first instance both the constructors called while for other instances only default one will be called.
  • Access modifiers and parameters are not allowed in the static constructor.
  • It initializes the static members of the class.
  • A static constructor is used to performing the operation only for one time.
  • Sometimes, for better performance, it is recommended to avoid static constructor.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. This will call the default constructor automatically  
  10.             Operations Operations = new Operations();  
  11.             Operations.Display();  
  12.   
  13.             // Another Instance of Operations class. This will call the parameterized constructor that has three parameters(two i               nt and one string)  
  14.             Operations operations1 = new Operations(22, 33, "heyyy");  
  15.             operations1.Display();  
  16.   
  17.   
  18.             // Another Instance of Operations class. This will call the Copy constructor  
  19.             Operations operations2 = new Operations(operations1);  
  20.             operations2.Display();  
  21.   
  22.             // Another Instance of static class. This will call the static constructor  
  23.             Operations operations3 = new Operations(Operations);  
  24.             operations3.Display();  
  25.   
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29.   
  30.     class Operations  
  31.     {  
  32.         public int Num1;  
  33.         public int Num2;  
  34.         public static int Static_num1;  
  35.         public static int Static_num2;  
  36.         public string str;  
  37.   
  38.         // Default Constructor. Constructor name is always same with class name  
  39.         public Operations()  
  40.         {  
  41.   
  42.             Num1 = 100;  
  43.             Num2 = 200;  
  44.             str = "Hello Buddy";  
  45.         }  
  46.   
  47.         // Parametrized Constructor. It is Default Constructor will parameters.  
  48.         // Parametrized Constructor must contain atleast one parameter else it will become Default constructor  
  49.         public Operations(int x, int y, string welcome_str)  
  50.         {  
  51.   
  52.             Num1 = x;  
  53.             Num2 = y;  
  54.             str = welcome_str;  
  55.         }  
  56.   
  57.         // Copy Constructor. It contain parameter of its type.  
  58.         public Operations(Operations ops)  
  59.         {  
  60.   
  61.             Num1 = ops.Num1;  
  62.             Num2 = ops.Num2;  
  63.             str = ops.str;  
  64.         }  
  65.   
  66.         static Operations()  
  67.         {  
  68.             Static_num1 = 565;  
  69.             Static_num2 = 22;  
  70.             Console.WriteLine("value of static variables are " + Static_num1 + " and " + Static_num2);  
  71.         }  
  72.   
  73.         // Compiler will produce a compile time error because parameters and access modifiers are not allowed on static contructo               r.  
  74.         // Error as "'Operations.Operations(int)': access modifiers are not allowed on static constructors and 'Operations.Operat            ions(int)': a static constructor must be parameterless"  
  75.         // Remove this constructor if running the code while copy and paste.  
  76.         public static Operations(int ops1)  
  77.         {  
  78.   
  79.         }  
  80.   
  81.         public void Display()  
  82.         {  
  83.             Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " and string = " + str);  
  84.         }  
  85.   
  86.   
  87.     }  
  88. }  
Output
  1. value of static variables are 565 22  
  2. Value of Num1 = 100 ,Num2 = 200 and string = Hello Buddy  
  3. Value of Num1 = 22 ,Num2 = 33 and string = heyyy  
  4. Value of Num1 = 22 ,Num2 = 33 and string = heyyy  
  5. Value of Num1 = 100 ,Num2 = 200 and string = Hello Buddy  
In the above program, we created a static constructor along with default, parameterized and copy constructors. Static constructor will be called only once when the first instance is created. This constructor is mostly used to assign value to static members of the class. We cannot create more than one static constructor. Also parameters and access modifiers are not allowed on static constructor. This will generate a compile time error as "'Operations.Operations(int)': access modifiers are not allowed on static constructors and 'Operations.Operations(int)': a static constructor must be parameterless".
 
Private Constructor
  • Private Constructor is constructor with private access modifier.
  • An instance of the private constructor cannot be created.
  • Class with private constructor can not be inherited.
  • Private constructor is used so as to restrict the class from being instantiated.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             // Instance of Operations class. Default constructor is private  
  10.             // Compiler will generate an error as "'Operations.Operations(int)' is inaccessible due to its protection level"  
  11.             Operations Operations = new Operations();  
  12.             Operations.Display();  
  13.   
  14.             // Another Instance of Operations class. Parameterized constructor is private  
  15.             // Compiler will generate an error as "'Operations.Operations(int)' is inaccessible due to its protection level"  
  16.             Operations operations1 = new Operations(22, 33, "heyyy");  
  17.             operations1.Display();  
  18.   
  19.   
  20.             // Another Instance of Operations class. Copy constructor is private  
  21.             // Compiler will generate an error as "'Operations.Operations(int)' is inaccessible due to its protection level"  
  22.             Operations operations2 = new Operations(operations1);  
  23.             operations2.Display();  
  24.   
  25.             // Another Instance of static class. Static constructor is private  
  26.             // Compiler will generate an error as "'Operations.Operations(int)' is inaccessible due to its protection level"  
  27.             Operations operations3 = new Operations(Operations);  
  28.             operations3.Display();  
  29.   
  30.             Operations.Static_num1 = 22;  
  31.             Operations.Static_num2 = 444;  
  32.   
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36.   
  37.     class Operations  
  38.     {  
  39.         public int Num1;  
  40.         public int Num2;  
  41.         public static int Static_num1;  
  42.         public static int Static_num2;  
  43.         public string str;  
  44.   
  45.         // Default Constructor. Constructor name is always same with class name  
  46.         private Operations()  
  47.         {  
  48.   
  49.             Num1 = 100;  
  50.             Num2 = 200;  
  51.             str = "Hello Buddy";  
  52.         }  
  53.   
  54.         // Parametrized Constructor. It is Default Constructor will parameters.  
  55.         // Parametrized Constructor must contain atleast one parameter else it will become Default constructor  
  56.         private Operations(int x, int y, string welcome_str)  
  57.         {  
  58.   
  59.             Num1 = x;  
  60.             Num2 = y;  
  61.             str = welcome_str;  
  62.         }  
  63.   
  64.         // Copy Constructor. It contain parameter of its type.  
  65.         private Operations(Operations ops)  
  66.         {  
  67.   
  68.             Num1 = ops.Num1;  
  69.             Num2 = ops.Num2;  
  70.             str = ops.str;  
  71.         }  
  72.   
  73.         // static Constructor. It must not contain access modifier and parameters.  
  74.         static Operations()  
  75.         {  
  76.             Static_num1 = 565;  
  77.             Static_num2 = 22;  
  78.             Console.WriteLine("value of static variables are " + Static_num1 + " and " + Static_num2);  
  79.         }  
  80.   
  81.         public void Display()  
  82.         {  
  83.             Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " and string = " + str);  
  84.         }  
  85.   
  86.   
  87.     }  
  88. }  
In the above program, we have created a private constructor. Private constructors can neither be instantiated nor be inherited. While creating an instance of private constructor, the compiler will produce an error as "'Operations.Operations(int)' is inaccessible due to its protection level". We are able to access static members only with this type of constructor.
 
Conclusion
 
Constructor is the very first method called whenever an instance of a class is created. Each type of constructor has its unique capabilities and uses. I hope this blog helps you to understand a bit more about Constructor.
 
Thank you. Please feel free to ask any question or make a suggestion.
Ebook Download
View all
Learn
View all