What Are Constructors?

Constructors are a particular type of method associated with a class and gets automatically invoked when the classes instance (i.e., objects) are created. Like other member functions, i.e., methods, these constructors also contain specific instructions that get executed when the objects are created. It is specifically used to assign values to all or some data members of a class.



Some Special Characteristics of the Constructor:

  • The name of the constructors must have to be the same as that of the class's name in which will resides.
  • A constructor can never be final, abstract, synchronized, and static.
  • You can produce only a single static constructor.
  • A static constructor cannot be used as a parameterized constructor.
  • Constructors usually don't have a return type, not even void.
  • The number of constructors can be any within a class.
  • Constructors can contain access modifiers along with it.

Types of Constructors in C#

  1. Default Constructor: When constructors do not have parameters, then it is called the default constructor. These types of constructors have all its instance initialized with the same value.
  2. Parameterized Constructor: When any constructor has at least one parameter, it is called the parameterized constructor.
  3. Copy Constructor: When the constructor is used to create an object just by copying all of its variables from another object, such constructors are called copy constructor. They are used for initializing a new instance from an existing one.
  4. Private Constructor: When a constructor is produced with a private access modifier, it is called Private Constructor. It does not make it possible for other classes to inherit any data from this class.
  5. Static Constructor: When a constructor needs to be invoked only once, and when that constructor needs to be invoked at creating the first reference, then those constructors are made static and are called static constructors.

C# Program to Demonstrate Constructor

Example:

using System;
namespace KarlosData
{
    class EmpGkr
    {
        private string month; 
        private int year; 
        public string name, location;
    
        //Default Constructor
        public EmpGkr()
        {
            name = "Alex Len";
            location = "San Francisco";
        }
    
        //Parameterized Constructor
        public EmpGkr(string a, string b)
        {
            name = a;
            location = b;
        }
        
        public EmpGkr(EmpGkr s) 
        { 
            month = s.month; 
            year = s.year; 
        } 
}

class Program
{
    static void Main(string[] args)
    {
        EmpGkr empGkr = new EmpGkr();
        EmpGkr u2 = new EmpGkr(empGkr); 
        Console.WriteLine(empGkr.name);
        Console.WriteLine(empGkr.location);
        Console.WriteLine("\n Press to Exit .... ");
        Console.ReadLine();
    }
}
}

Output:

Alex Len
San Francisco

What Are Destructors?

Destructor is another method that uses the class-name but is preceded with a ~ (tilde) operator/symbol. Destructors are used to de-initialize objects, and the memories occupied when constructors are created. You can consider them as the opposite of constructors.

C# Program to Demonstrate Destructor

public class EmpGkr
{
    int start;
    //constructor
    public EmpGkr()
    {
        val = 10;
    }

    //destructor
    ~ EmpGkr()
    {
        val = 0;
    }
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram