问题
Is there any downside of calling GC.SuppressFinalize(object)
multiple times?
Protected Dispose(bool)
method of the dispose pattern checks whether it is called before but there is no such check in the public Dispose()
method.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_Disposed)
return;
if (disposing)
{
// Cleanup managed resources.
}
// Cleanup unmanaged resources.
_Disposed = true;
}
~MyClass() { Dispose(false); }
Is it ok to call the Dispose()
method of a MyClass
instance multiple times?
回答1:
According to docs: http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx, it sets some bit in object header, so there shouldn't be any implications of calling it multiple times.
来源:https://stackoverflow.com/questions/12436555/calling-suppressfinalize-multiple-times