Creating Destructors and Handling Garbage Collection

The flip side of constructors are destructors, which are called when it's time to get rid of an object and perform cleanup, such as disconnecting from the Internet or closing files. Getting rid of no-longer-needed objects involves the C# garbage collector, which calls your destructor. The C# garbage collector is far more sophisticated than the ones available in most C++ implementations, as we'll see soon. Let's start with destructors first.

Creating a Destructor

You place the code you want to use to clean up an object when it's being deleted, if any, in a destructor. Destructors cannot be static, cannot be inherited, do not take parameters, and do not use access modifiers. They're declared ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.