How to: Use the disposable pattern

One of the Microsoft guidelines is to use the disposable pattern on objects.

A class becomes disposable by implementing the interface IDisposable:

The method Dispose contains the implementation of the IDisposable interface, the property IsDisposed tells if the object has been disposed already.
The finalizer contains the code that has to be executed if the GC or reference counting causes the object to be destroyed and the dispose code did not execute.

A standard implemention for the dispose pattern is:

Usage pattern, which will dispose the object as soon as you are done with it:



No comments:

Post a Comment