use if required

Remarks

  • Destructors cannot be defined in structs. They are only used with
    classes.

  • A class can only have one destructor.

  • Destructors cannot be inherited or overloaded.

  • Destructors cannot be called. They are invoked automatically.

  • A destructor does not take modifiers or have parameters.

For example, the following is a declaration of a destructor for the class
Car:

class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}

The destructor implicitly calls Finalize
on the base class of the object. Therefore, the previous destructor code is
implicitly translated to the following code:

Using Destructors to Release Resources

In general, C# does not require as much memory management as is needed when
you develop with a language that does not target a runtime with garbage
collection. This is because the .NET Framework garbage collector implicitly
manages the allocation and release of memory for your objects. However, when
your application encapsulates unmanaged resources such as windows, files, and
network connections, you should use destructors to free those resources. When
the object is eligible for destruction, the garbage collector runs the Finalize method of the object.

 Explicit Release of Resources

If your application is using an expensive external resource, we also
recommend that you provide a way to explicitly release the resource before the
garbage collector frees the object. You do this by implementing a Dispose method from the IDisposable
interface that performs the necessary cleanup for the object. This can
considerably improve the performance of the application. Even with this explicit
control over resources, the destructor becomes a safeguard to clean up resources
if the call to the Dispose method failed

http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s