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