O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

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

2.18. The Class Destructor

C# supports a nondeterministic destructor method, but its use is discouraged. It is nondeterministic in two respects. The first is that we are unable to predict when the destructor is called, or even if it is called. Second, we are unable to predict the order of destructor invocations. From a performance standpoint, classes with destructors have significantly greater overhead in their interaction with the garbage collector.

A destructor—or some sort of finalization routine—is necessary when a class object acquires unmanaged resources during its lifetime and we wish those resources to be released following a last use of that object. Resources might include window or file handles, or database connections. By convention, ...

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