Why should we call SuppressFinalize when we don’t have a destructor

I’m going out on a limb here, but… most people don’t need the full-blown dispose pattern. It’s designed to be solid in the face of having direct access to unmanaged resources (usually via IntPtr) and in the face of inheritance. Most of the time, neither of these is actually required. If you’re just holding a … Read more

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

tech