I think you must not care about disposing your class except if you have unmanaged resources. Here is an example where it actually useful:
public class ComplexResourceHolder : IDisposable
{
private IntPtr buffer; // unmanaged memory buffer
private SafeHandle resource; // disposable handle to a resource
public IntPtr Buffer { get { return buffer; } set { buffer = value; } }
public ComplexResourceHolder()
{
this.buffer = ... // allocates memory
this.resource = ... // allocates the resource
}
protected virtual void Dispose(bool disposing)
{
ReleaseBuffer(buffer); // release unmanaged memory
if (disposing)
{
// release other disposable objects
if (resource!= null)
resource.Dispose();
}
}
~ ComplexResourceHolder(){
Dispose(false);
}
public void Dispose(){
Dispose(true);
GC.SuppressFinalize(this);
}
}
Check the MSDN for better understanding of Dispose(bool) finalizer override. And this link about unmanaged resources will be useful as well because this is the first reason why you should use IDisposable.
You might wonder to use constructions like below if the class inherits IDisposable:
using (ComplexResourceHolder crh = new ComplexResourceHolder())
{
//Do something with buffer for an instance
//crh.Buffer =
}
Dispose method will be called automatically after closing tag '}'.