I know that since some version StreamWriter
stopped support Finalizer, but in source code I see it calls GC.SuppressFinalize(this)
. Why does it call it? I thought it is actually only for the object which has Finalizer.
Update
I am wondering whether GC.SuppressFinalize()
has some effect if the object has not Finalizer?
Its part of the dispose pattern.
Microsoft describes the dispose pattern to look like:
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
To quote the link:
The
Dispose
method performs all object cleanup, so the garbage collector no longer needs to call the objects'Object.Finalize
override. Therefore, the call to theSuppressFinalize
method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call toGC.SuppressFinalize
has no effect. Note that the actual cleanup is performed by theDispose(bool)
method overload.
edit:
Looking further, with regards to DRY, I would StreamWriter.Close()
just to call StreamWrite.Dispose()
. Furthermore, StreamWriter.Close()
seems redundant anyhow, as the base class TextWriter.Close()
has the same contents! (And that one should directly call TextWrite.Dispose()
.
But that's just MHO.)