A destructor/finalizer cannot be called directly. It is called automatically when no references to the object remain AND the Garbage Collector (GC) runs.
If you want to free resources you should implement the dispose pattern implementing
IDisposable
(as below) then consumers of your class have two choices:
1. Call Dispose() when they're done
2. Use a using block and it will be called automatically
public class YourClass : IDisposable
{
private bool isDisposed;
~YourClass()
{
Dispose(false);
}
public bool IsDisposed
{
get{ return isDisposed; }
}
public void Dispose()
{
Dispose(true);
GC.SupressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if(disposing)
{
}
isDisposed = true;
}
}
}