In this article, I am going to share with you about Constructor and its types in C#
What is Constructor?
- A constructor is a special method of a class, that is called automatically when an object/instance of the class is created.
- Constructor name is always the same as the class name. If a class name is 'A', then the constructor of this class is 'A()'.
- A class can contain any number of constructors, but with different arguments.
- 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'.
- A constructor does not have any return type (any data type like int, bool, string, void etc).
- 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.
- A class can contain only one static constructor.
- The constructor can be of five types. The syntax of each type is a bit different.
- We use 'base' keyword to call the constructor of a base class when inherited.
Constructors can be of 5 types
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Operations Operations = new Operations();
-
- Operations.Addition(33, 33);
-
-
- Operations operations2 = new Operations();
- operations2.Addition(133, 33);
- operations2.Subtraction(100, 5);
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public double result;
- public string str;
-
-
- public Operations()
- {
-
- Num1 = 100;
- Num2 = 200;
- str = "Hello Buddy";
- }
-
-
- public double Addition(int a, int b)
- {
- result = a + b;
- Console.WriteLine("Result of Addition = " + result);
- return result;
- }
-
-
- public double Subtraction(int a, int b)
- {
- result = a - b;
- Console.WriteLine("Result of Subtraction = " + result);
- return result;
- }
-
- }
- }
Output
- Result of Addition = 66
- Result of Addition = 166
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Operations Operations = new Operations();
-
- Operations.Display();
-
-
- Operations operations2 = new Operations();
- operations2.Display();
- operations2.Subtraction(100, 5);
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public double result;
- public string str;
-
- public void Display()
- {
- Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);
- }
-
- public double Addition(int a, int b)
- {
- result = a + b;
- Console.WriteLine("Result of Addition = " + result);
- return result;
- }
-
- public double Subtraction(int a, int b)
- {
- result = a - b;
- Console.WriteLine("Result of Subtraction = " + result);
- return result;
- }
-
- }
- }
Output
- Value of Num1 = 0 ,Num2 = 0 ,Result = 0 and string =
- Value of Num1 = 0 ,Num2 = 0 ,Result = 0 and string =
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Operations Operations = new Operations();
- Operations.Display();
-
-
- Operations operations1 = new Operations(22, 33, "heyyy");
- operations1.Display();
-
-
- Operations operations2 = new Operations(4, 66);
- operations2.Display();
-
-
- Operations operations3 = new Operations(4);
- operations3.Display();
-
-
- Operations operations4 = new Operations(4, 77, 6);
- operations4.Display();
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public double result;
- public string str;
-
-
- public Operations()
- {
-
- Num1 = 100;
- Num2 = 200;
- str = "Hello Buddy";
- }
-
-
-
- public Operations(int x, int y, string welcome_str)
- {
-
- Num1 = x;
- Num2 = y;
- str = welcome_str;
- }
-
-
- public Operations(int x, int y)
- {
-
- Num1 = x;
- Num2 = y;
- str = "Hello Buddy";
- }
-
-
- public Operations(int x)
- {
-
- Num1 = x;
- Num2 = 500;
- str = "Hello Buddy";
- }
-
-
- public Operations(int x, int y, int z)
- {
-
- Num1 = x;
- Num2 = y;
- str = "Hello Buddy";
- }
-
- public void Display()
- {
- Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);
- }
-
-
- public double Addition(int a, int b)
- {
- result = a + b;
- Console.WriteLine("Result of Addition = " + result);
- return result;
- }
-
-
- public double Subtraction(int a, int b)
- {
- result = a - b;
- Console.WriteLine("Result of Subtraction = " + result);
- return result;
- }
-
- }
- }
Output
- Value of Num1 = 100 ,Num2 = 200 ,Result = 0 and string = Hello Buddy
- Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = h the yyy
- Value of Num1 = 4 ,Num2 = 66 ,Result = 0 and string = Hello Buddy
- Value of Num1 = 4 ,Num2 = 500 ,Result = 0 and string = Hello Buddy
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Operations Operations = new Operations();
- Operations.Display();
-
-
- Operations operations1 = new Operations(22, 33, "heyyy");
- operations1.Display();
-
-
-
- Operations operations2 = new Operations(operations1);
- operations2.Display();
-
-
- Operations operations3 = new Operations(Operations);
- operations3.Display();
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public double result;
- public string str;
-
-
- public Operations()
- {
-
- Num1 = 100;
- Num2 = 200;
- str = "Hello Buddy";
- }
-
-
-
- public Operations(int x, int y, string welcome_str)
- {
-
- Num1 = x;
- Num2 = y;
- str = welcome_str;
- }
-
-
- public Operations(Operations ops)
- {
-
- Num1 = ops.Num1;
- Num2 = ops.Num2;
- str = ops.str;
- }
-
-
- public void Display()
- {
- Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " ,Result = " + result + " and string = " + str);
- }
-
-
- }
- }
Output
- Value of Num1 = 100 ,Num2 = 200 ,Result = 0 and string = Hello Buddy
- Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = heyyy
- Value of Num1 = 22 ,Num2 = 33 ,Result = 0 and string = heyyy
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Operations Operations = new Operations();
- Operations.Display();
-
-
- Operations operations1 = new Operations(22, 33, "heyyy");
- operations1.Display();
-
-
-
- Operations operations2 = new Operations(operations1);
- operations2.Display();
-
-
- Operations operations3 = new Operations(Operations);
- operations3.Display();
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public static int Static_num1;
- public static int Static_num2;
- public string str;
-
-
- public Operations()
- {
-
- Num1 = 100;
- Num2 = 200;
- str = "Hello Buddy";
- }
-
-
-
- public Operations(int x, int y, string welcome_str)
- {
-
- Num1 = x;
- Num2 = y;
- str = welcome_str;
- }
-
-
- public Operations(Operations ops)
- {
-
- Num1 = ops.Num1;
- Num2 = ops.Num2;
- str = ops.str;
- }
-
- static Operations()
- {
- Static_num1 = 565;
- Static_num2 = 22;
- Console.WriteLine("value of static variables are " + Static_num1 + " and " + Static_num2);
- }
-
-
-
-
- public static Operations(int ops1)
- {
-
- }
-
- public void Display()
- {
- Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " and string = " + str);
- }
-
-
- }
- }
Output
- value of static variables are 565 22
- Value of Num1 = 100 ,Num2 = 200 and string = Hello Buddy
- Value of Num1 = 22 ,Num2 = 33 and string = heyyy
- Value of Num1 = 22 ,Num2 = 33 and string = heyyy
- 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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- Operations Operations = new Operations();
- Operations.Display();
-
-
-
- Operations operations1 = new Operations(22, 33, "heyyy");
- operations1.Display();
-
-
-
-
- Operations operations2 = new Operations(operations1);
- operations2.Display();
-
-
-
- Operations operations3 = new Operations(Operations);
- operations3.Display();
-
- Operations.Static_num1 = 22;
- Operations.Static_num2 = 444;
-
- Console.ReadLine();
- }
- }
-
- class Operations
- {
- public int Num1;
- public int Num2;
- public static int Static_num1;
- public static int Static_num2;
- public string str;
-
-
- private Operations()
- {
-
- Num1 = 100;
- Num2 = 200;
- str = "Hello Buddy";
- }
-
-
-
- private Operations(int x, int y, string welcome_str)
- {
-
- Num1 = x;
- Num2 = y;
- str = welcome_str;
- }
-
-
- private Operations(Operations ops)
- {
-
- Num1 = ops.Num1;
- Num2 = ops.Num2;
- str = ops.str;
- }
-
-
- static Operations()
- {
- Static_num1 = 565;
- Static_num2 = 22;
- Console.WriteLine("value of static variables are " + Static_num1 + " and " + Static_num2);
- }
-
- public void Display()
- {
- Console.WriteLine("Value of Num1 = " + Num1 + " ,Num2 = " + Num2 + " and string = " + str);
- }
-
-
- }
- }
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.