Destructors

The purpose of destructors is to implement finalization code when an object goes out of scope or is destroyed. Because of the way the CLR garbage collector handles objects, there is the distinct possibility of a destructor not being called. Furthermore, there is no precise time after which your object is destroyed that a destructor is called. Here's an example of a destructor:

public class WebSite
{

    string siteName;
    string url;
    string description;

    public WebSite(string newSite,
                   string newURL,
                   string newDesc)
    {
        siteName    = newSite;
        url         = newURL;
        description = newDesc;
    }

    ~WebSite() {}
}

The destructor in the example shows a typical implementation. All destructors begin with the tilde (~) symbol. Their names are the same as their enclosing ...

Get C# Unleashed 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.