Is it really better not to use finalize compare to dispose ? Does dispose remove unmanaged resource in first parse ?
What is suppressing finalize ?
Your implementation of IDisposable should be:
public MyClass : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected Dispose(bool disposing)
{
if( disposing )
{
// release unmanaged resource
}
// release managed resources
}
~MyClass()
{
Dispose(false);
}
}
If an object has a finalizer the CLR keeps a reference to it in the finalization queue. If you dispose manually the object (calling Dispose()) you already remove the unmanaged resources (as the implementation of dispose and the finalizer is shared), so there is no need to call the finalizer and you can safely remove the object from the finalization queue (calling GC.SuppressFinalize(this)).