c# Concepts – Constructor and Types of Constructor

In this article I will explain you what is constructor & types of constructor with examples.

Description:

  • Constructor is special type of method which is automatically invoked when instance/object of class is made.
  • If constructor is not defined for class then compiler will automatically create one for that class when creating object/instance of that class.
  • Constructors are used for object initialization and memory allocation of its class
  • Class can have any number of constructors but we can create only one static constructor in a class.
  • Constructors do not have any return types.
  • Constructor have same name as that of its class.
    E.g.

    class Abc {
       public Abc(){
           Console.WriteLine("Default Constructor");
       }
    }
    

Types of Constructor:
There are 5 types of constructors

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor

Default Constructor

  • Default Constructor is constructor without any parameters.
  • E.g.
    class Abc {
       public Abc(){
           Console.WriteLine("Default Constructor");
       }
    }
    class Program{
        static void Main(string[] args){
           Abc obj = new Abc(); 
        }
    }
    

Parameterized Constructor

  • Parameterized Constructor is constructor with atleast one patameter.
  • E.g.
    class Abc {
       public Abc(int param1){
           Console.WriteLine(param1);
       }
    }
    class Program{
        static void Main(string[] args){
           Abc obj = new Abc(10); 
        }
    }
    

Copy Constructor

  • Copy Constructor is a parameterized constructor having class object type as paramterer
  • Purpose of Copy Constructor is to create new instance of class with same values.
  • E.g.
    class Abc {
       public int Param1;
       public Abc(int x){
           Param1 = x;
       }
       public Abc(Abc obj){
           Param1 = obj.Param1;
       }
    
    }
    class Program{
        static void Main(string[] args){
           Abc obj = new Abc(10); 
           Abc obj2 = new Abc(obj); 
        }
    }
    

Static Constructor

  • Static Constructor is a constructor which is invoked during creation of first instance of class.
  • Static Constructor is used to initialize static fields.
  • E.g.
    class Abc {
        static Abc(){
           Console.WriteLine("Static Constructor");
       }
       public Abc(){
           Console.WriteLine("Default Constructor");
       }
    }
    class Program{
        static void Main(string[] args){
           Abc obj = new Abc(); // both static & default constructors are invoked
    Abc obj2 = new Abc(); // only default construcotr is invoked
        }
    }
    

Private Constructor

  • Private constructor is a special instance constructor used in a class that contains static member only
  • E.g.
    class Abc {
       public int x;
       private Abc(){
           Console.WriteLine("Private Constructor");
       }
    public Abc(int a){
           x = a;
           Console.WriteLine("Parameterized Constructor");
       }
    }
    class Program{
        static void Main(string[] args){
           Abc obj = new Abc(10); // it will work fine. 
           Abc obj = new Abc();   // it will give error as constructor without parameters is private.
        }
    }
    

admin