Looking at the sample code of MSDN
// Design pattern for a base class.
public class Base: IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true); <---- 1 Here
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
disposed = true;
}
}
// Use C# destructor syntax for finalization code.
~Base()
{
// Simply call Dispose(false).
Dispose (false);
}
}
I don't seem to get the line of code pointed in the first arrow (<----- 1 Here).
I was confused in the first arrow as to Whom the Dispose belong. to the IDisposable or the virtual Dispose of the base?
Any Help that would help me is Great and Very much appreciated!
In that case, the Dispose belongs to the caller aka dev.
This is never automatically called, but always by the developer. The
true
flag indicates that the dispose was explicitly called.GC.SuppressFinalize(this);
tells the Garbage Collector that the disposal of that object is already taken care of. No need to call the finalize block.This is automatically called by the Garbage Collector if the object has not been explicitly disposed. It is undetermined when exactly it is called, but sometime when the object has gone out of scope and is marked to be garbage collected. The
false
flag indicates that this is an automatic disposal in which case managed objects no longer need to be explicitly disposed.Also, an additional note about finalizers taken from Ben Watson's Writing High-Performance .NET Code