Base object for disposing managed and unmanaged objects. This object implements the dispose pattern for the .NET Framework.
Create a class and inherit from DisposableObject as shown below:
using System;
public class SomeObject : DisposableObject
{
protected override void OnDisposeManagedObjects()
{
//
// Disposed CLR managed objects here.
//
}
protected override void OnDisposeUnmanagedObjects()
{
//
// Disposed non-CLR managed objects here.
//
}
}
Override one or both of the above methods depending on the type of resources your class uses.
When instantiating the class. wrap it in a using statement.
using (SomeObject obj = new SomeObject())
{
//
// obj will be disposed and the dispose
// methods will be called.
}
or call the Dispose() method.
SomeObject obj = new SomeObject();
obj.Dispose();
In either case, the OnDisposeManagedObjects() and OnDisposeUnmanagedObjects() are called.