7.5. Destructors

A destructor is a special method which is somewhat similar to Java's finalizer method. [7] Traditionally, in C++ codes, clean-up code is placed in the destructor. The destructor for an instance is called automatically during garbage collection, when the instance is destroyed. You usually release resources not managed by the .NET runtime in the destructor (such as file or database connections).

[7] Destructors came from C++. In C++ classes, destructors are called when the object is manually deleted using the C++ delete keyword. That's where you would recursively delete other objects created and referenced exclusively by the current object. Poor destructor programming in C++ leads to memory-leaky applications. Unlike C++, C# (and ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.