Constructors and Destructors in C#

Mannan Ul Haq
0

Constructors:

Constructors are special member functions of a class that are called automatically when an object of that class is created. Constructors are used to initialize the data members of an object.


A constructor has the same name as the class and no return value.


Types of Constructors:

1. Default Constructor:

  • A default constructor is a constructor that is automatically generated by the compiler if no constructor is explicitly defined in the class.
  • It has no parameters and initializes the data members to their default values.
  • The default constructor is invoked when an object is created without any arguments.

Example:

class MyClass
{
    private int data;

    // Default constructor (automatically generated if not defined)
    public MyClass()
    {
        data = 0;
    }
}

 

In the above example, the `MyClass` has a default constructor that initializes the `data` member to zero.

2. Parameterized Constructor:

  • A parameterized constructor is a constructor that takes one or more parameters.
  • It allows you to initialize the data members of an object with specific values provided at the time of object creation.

Example:

class Rectangle
{
    private int length;
    private int width;

    // Parameterized constructor
    public Rectangle(int len, int wid)
    {
        length = len;
        width = wid;
    }
}

In the above example, the `Rectangle` class has a parameterized constructor that takes the length and width as parameters and initializes the corresponding data members.

3. Constructors with Default Arguments:

  • Constructors can have default arguments for one or more parameters.
  • Default arguments allow you to create objects without providing explicit values for those parameters.

Example:

class Square
{
    private int side;

    // Constructor with default argument
    public Square(int s = 0)
    {
        side = s;
    }
}

In the above example, the `Square` class has a constructor with a default argument for the `s` parameter. If no argument is provided during object creation, the side is initialized to zero.

Constructor Overloading:

Constructor overloading refers to the ability to define multiple constructors within a class, each having a different parameter list. The compiler determines which constructor to invoke based on the arguments provided during object creation.

Example:

class Employee
{
    private string name;
    private int age;
    private double salary;

    // Default constructor
    public Employee()
    {
        name = "";
        age = 0;
        salary = 0.0;
    }

    // Parameterized constructor with three arguments
    public Employee(string newName, int newAge, double newSalary)
    {
        name = newName;
        age = newAge;
        salary = newSalary;
    }

    // Constructor with default arguments
    public Employee(string newName, int newAge = 0, double newSalary = 0.0)
    {
        name = newName;
        age = newAge;
        salary = newSalary;
    }
}

In the above example, the `Employee` class demonstrates constructor overloading. It has a default constructor, a parameterized constructor, and a constructor with default arguments. These constructors allow creating `Employee` objects with different initialization options.

Destructors:

  • In C#, destructors are called finalizers. Unlike constructors, destructors don't need to be explicitly defined, and you cannot overload them. The garbage collector automatically releases the resources held by objects when they are no longer needed.
  • Destructor has the same name as their class name preceded by a tilde (~) symbol.

Example:

class Car
{
    private string brand;

    // Constructor
    public Car(string carBrand)
    {
        brand = carBrand;
    }

    // Destructor (Finalizer)
    ~Car()
    {
        // Clean up resources, if needed
    }
}

In the above example, the `Car` class has a destructor that is invoked automatically when a `Car` object is destroyed. The destructor can be used to release any resources held by the object, such as closing files or freeing allocated memory.

NOTE: It's essential to note that C# provides automatic memory management through its garbage collector, so explicit memory deallocation using destructors (as in C++) is not typically required. (alert-success)

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !