What’s the purpose of GC.SuppressFinalize(this) in Dispose() method?

When implementing the dispose pattern you might also add a finalizer to your class that calls Dispose(). This is to make sure that Dispose() always gets called, even if a client forgets to call it. To prevent the dispose method from running twice (in case the object already has been disposed) you add GC.SuppressFinalize(this);. The … Read more

When should I use GC.SuppressFinalize()?

SuppressFinalize should only be called by a class that has a finalizer. It’s informing the Garbage Collector (GC) that this object was cleaned up fully. The recommended IDisposable pattern when you have a finalizer is: public class MyClass : IDisposable { private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!disposed) { … Read more