O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required