Constructor
It is a special method available to a class responsible for initializing the variables of a class.
The Constructor method will exactly have the same name as your class and this method does not return a method.
Every class requires a constructor if the class must execute at all and without a constructor in a class the execution of the class will not happen. So it is mandatory for a class to execute, because a constructor is mandatory for a class to execute. It must be either explicitly defined or will be implicitly defined when compiling the program.
If a class is defined as in the following:
- Class demo
- {
- Int x;string s;bool b;
- }
Then after compiliation it will be as if the following exists for the class:
- Class demo
- {
- Int x;string s;bool b;
- Public demo()
- {
- X=0;s=null;b=false;
- }
- }
Whenever we have created an object of a class we explicitly call the constructor for execution, for example:
demo obj=new demo();
Whenever the constructor of a class is called the memory required for execution of the class is allocated.
When the references of a class are created then the constructor will not be called so there will not be memory allocation since we are already have a reference or pointers to the object consuming the memory of the object that is assigned to it.
So the references are also used for accessing the member.
The following are the types of constructors:
- Zero Argument Constructor
- Parameteried Consturctor
1. Zero Argument Constructor
A constructor without any parameters is called a zero argument constructor.
The constructor is basically for initializing the variable of a class with default values so this constructor is also refered to as a “Default Constructor”.
A default constructor can be defined explicitly by programmers or will be defined implicitly at the time of the compiler provided a class doesn't contain any constructor in it.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Constructor_Demo
- {
- class Constructor_Demo
- {
-
- public Constructor_Demo()
- {
- Console.WriteLine("this is constructor");
-
- }
- public void methode()
- {
- Console.WriteLine("this is method");
-
- }
- static void Main(string[] args)
- {
- Constructor_Demo obj = new Constructor_Demo();
- obj.methode();
- Console.ReadKey();
- }
- }
- }
2. Parameterized Constructor
A constructor with a parameter is referred to as a parameterized constructor and we use them for initializing variables of the class with user-defind values, that is we can pass other values for initializing the class variables.
A Parameterized constructor can be defind explicitly only.
If a constructor has parameterized values then the parameters must be supplied when creating the object.
For example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Constructor_Demo
- {
- class DefaultandparemetixedCon
- {
- string name;
-
- public DefaultandparemetixedCon()
- {
- name = "ram";
-
- }
-
- public DefaultandparemetixedCon(string Name)
- {
- this.name = Name;
- }
-
- public void showname()
- {
- Console.WriteLine("Name =" + name);
- }
- static void Main()
- {
-
- DefaultandparemetixedCon obj = new DefaultandparemetixedCon();
- obj.showname();
-
- DefaultandparemetixedCon obj1 = new DefaultandparemetixedCon("Abhijit");
- obj1.showname();
-
- Console.ReadKey();
- }
- }
- }