Constructor in C#

What is a constructor?

  • The constructors are the special methods that have the same name as the class name and used to initialize the global variables.

  • Every class by default have their own constructors even if the programmer doesn't write it explicitly.

  • Whenever we create the object what actually happening in the background is we are calling the constructor.

Note : Changes made using the one object reference will not effect other object reference.

// Topic: Constructor
using System;
class MainClass {
  // this class has its own Constructor even if the programmer doesn't write explicitly.
}

Types of Constructor in C#:

  1. Default Constructor

  2. Parameterized Constructor

  3. Copy Constructor

  4. Static Constructor

1. Default constructor :

  • This constructor is also known as Zero argument Constructors We can write this constructor as follows :
// Topic: Default Constructor
using System;
class Test {
  public Test()
  {
    Console.WriteLine("Default Constructor");
  }
}
class MainClass {
  public static void Main() {
    Test test = new Test();
  }
}
/*
Output :
Default Constructor
*/

2. Parameterized Constructor :

  • Parameterized constructor takes the arguments and initialize the value to the given instance of the object. This helps us to make every object different instead of hardcoding the values inside the class.

    // Topic: Parameterized Constructor
    using System;
    class Account {
    public Account(string name)
    {
      Console.WriteLine($"This account belongs to {name}");
    }
    }
    class MainClass {
    public static void Main() {
      Account a1 = new Account("Vishal");
      Account a2 = new Account("Pawar");
    }
    }
    /*
    Output :
    This account belongs to Vishal
    This account belongs to Pawar
    */
    

    3. Copy Constructor :

  • This constructor is used whenever we need a copy of the other object by copying the variable values from another object.

  • Copy Constructor is a parameterized constructor that takes object reference as an argument and copies the values of the variable and creates a new Object.

    // Topic: Copy Constructor
    using System;
    class Account {
    string name, bankName;
    public Account(string name, string bankName)
      {
      this.name = name;
      this.bankName = bankName;
      }
    public Account(Account ac)
      {
      Console.WriteLine("Copying values.....");
     this.name = ac.name;
     this.bankName = ac.bankName;
     Console.WriteLine("Copied values.....");
      }
    public void display()
      {
      Console.WriteLine($"The account holder name is {name} and bankname is {bankName}");
      }
    }
    class MainClass {
    public static void Main() {
      Account a1 = new Account("Vishal","Axis bank");
      a1.display();
      Account a2 = new Account(a1);
      a2.display();
      }
    }
    /*
    Output :
    The account holder name is Vishal and bankname is Axis bank
    Copying values.....
    Copied.....
    The account holder name is Vishal and bankname is Axis bank
    */
    

4. Static Constructor :

  • The static constructor is used to initialize the static variables of the class and the static constructor executes first whenever the object being created. and, We can't create a static parameterized constructor. Also, the static Constructor executes only once.

Note: We can't overload and pass arguments to the static constructor.

// Topic: static Constructor
using System;
class Account {
  static string bankName;
  //This class has default Constructor

  //static Constructor
  static Account()
  {
    Console.WriteLine("Static Constructor called");
    bankName = "Axis bank";
  }
}
class MainClass {
  public static void Main() {
    Account a1 = new Account();
    Account a2 = new Account();
  }
}
/*
Output :
Static Constructor called
*/

Note: In the above code the object created for the Account class was 2 times still the static constructor was executed one time only. By this we can tell the static constructor executes only once for n number of objects created.