Constructors in C#

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:

  1. Class demo  
  2. {  
  3.     Int x;string s;bool b;  

Then after compiliation it will be as if the following exists for the class:

  1. Class demo  
  2. {  
  3.     Int x;string s;bool b;  
  4.     Public demo()  
  5.     {  
  6.        X=0;s=null;b=false;  
  7.     }  

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:

  1. Zero Argument Constructor
  2. 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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Constructor_Demo  
  8. {  
  9.     class Constructor_Demo  
  10.     {  
  11.         //create the constructor  
  12.         public Constructor_Demo()  
  13.         {  
  14.             Console.WriteLine("this is constructor");  
  15.             //  
  16.         }  
  17.         public void methode()  
  18.         {  
  19.             Console.WriteLine("this is method");  
  20.               
  21.         }  
  22.         static void Main(string[] args)  
  23.         {  
  24.             Constructor_Demo obj = new Constructor_Demo();  
  25.             obj.methode();  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  

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:

  1. using System;    
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Constructor_Demo  
  8. {  
  9.     class DefaultandparemetixedCon  
  10.     {  
  11.         string name;  
  12.         //default constructor  
  13.         public DefaultandparemetixedCon()  
  14.         {  
  15.             name = "ram";  
  16.   
  17.         }  
  18.         //parameterized constructor  
  19.         public DefaultandparemetixedCon(string Name)  
  20.         {  
  21.             this.name = Name;  
  22.         }  
  23.         //method  
  24.         public void showname()  
  25.         {  
  26.             Console.WriteLine("Name =" + name);  
  27.         }  
  28.             static void Main()  
  29.         {  
  30.                 //default constructor  
  31.                 DefaultandparemetixedCon obj = new DefaultandparemetixedCon();  
  32.             obj.showname();  
  33.             //parameterized constructor  
  34.              DefaultandparemetixedCon obj1 = new DefaultandparemetixedCon("Abhijit");  
  35.             obj1.showname();  
  36.               
  37.             Console.ReadKey();  
  38.         }  
  39.     }  


Similar Articles