Article image Object Orientation in C#: Constructors and Destructors

16.7. Object Orientation in C#: Constructors and Destructors

Page 23 | Listen in audio

In object-oriented programming in C#, constructors and destructors are special methods that are automatically executed when an object is created and destroyed. They are essential for initializing and cleaning up object resources.

Builders

In C#, a constructor is a special method with the same name as the class. It is called automatically when an object is created. The constructor does not have a return type, not even void. This distinguishes it from other methods. The constructor is used to initialize the object's data fields and perform any other necessary configuration.

public class Game
{
    public string name;
    public Game(string name)
    {
        this.name = name;
    }
}

In this example, the Game class has a constructor that accepts a string parameter. When a Game object is created, the constructor is called with the value of the game name.

Game game = new Game("Super Mario");

Here, "Super Mario" is passed to the constructor of the Game class, and the name field of the game object is initialized to this value.

If we don't define a constructor for a class, the C# compiler will automatically create a default parameterless constructor for us. This default constructor will initialize all object fields to their default values.

Destructors

In C#, a destructor is a special method that is called automatically when an object is destroyed. The destructor has the same name as the class, preceded by a tilde (~). The destructor cannot have parameters or return types.

public class Game
{
    ~Game()
    {
        // code to clean up resources
    }
}

In this example, the Game class has a destructor. The code inside the destructor will be executed when the Game object is destroyed.

Destructors are used to release unmanaged resources that the object may have acquired during its lifetime. Unmanaged resources are resources that the .NET garbage collector cannot automatically clean up, such as file handles, database connections, or unmanaged memory buffers.

In C#, we don't need (and can't) explicitly call a destructor. The .NET garbage collector automatically calls the destructor when there are no more references to the object. However, we cannot predict when the garbage collector will perform garbage collection, so we cannot predict when the destructor will be called.

If we need to release unmanaged resources immediately, we can implement the Dispose method of the IDisposable interface. We can call Dispose explicitly when we are done using the object. Inside the Dispose method, we must free all unmanaged resources and call GC.SuppressFinalize(this) to tell the garbage collector that it does not need to call the destructor.

In short, constructors and destructors in C# are special methods that are automatically called when an object is created and destroyed. They are used to initialize and clean up object resources. Although the .NET garbage collector handles most of the memory management details for us, we still need to understand how and when to use constructors and destructors to manage resources effectively.

Now answer the exercise about the content:

What is the role of constructors and destructors in object-oriented programming in C#?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Object Orientation in C#: Methods and Properties

Next page of the Free Ebook:

24Object Orientation in C#: Methods and Properties

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text