Understanding C# Destructors and Finalization
Learn how C# destructors (finalizers) work, when they run, and why IDisposable is usually the better cleanup mechanism.

In C#, the term destructor is a legacy name for a finalizer. The syntax is a method with the same name as the class prefixed by a tilde, and it has no parameters, no access modifier, and no return type. When the garbage collector determines that an object is no longer reachable, the object is scheduled for finalization, and the finalizer then runs asynchronously on a runtime-managed finalizer thread. The exact timing is nondeterministic. This article explains how finalizers behave, why they are rarely the right tool for resource cleanup, and how they interact with IDisposable and the finalization queue.
Declaring a Finalizer in C#
The syntax for a finalizer is simple, but the semantics are easy to misunderstand. Consider a class that holds an unmanaged resource:
public class UnmanagedResource
{
private IntPtr _handle;
public UnmanagedResource()
{
_handle = SomeNativeAllocation();
}
~UnmanagedResource()
{
SomeNativeRelease(_handle);
}
}The finalizer is declared with ~ClassName(). It cannot be called directly from your code; the runtime invokes it through the finalizer thread. The compiler implements the finalizer as an override of the protected virtual Finalize method on System.Object, but you never write that method directly in C#.
Because finalization order is not guaranteed, do not access other managed objects from inside a finalizer. They may already have been finalized or may be in an indeterminate state. Also, do not rely on thread-specific state, because the finalizer runs on a runtime-managed finalizer thread, not on the thread that created the object.
When Does the Garbage Collector Run a Finalizer?
A finalizer runs only after the garbage collector determines that the object is unreachable. The exact moment is not predictable. The GC may run during a low-memory condition, when a generation threshold is hit, or when you explicitly call GC.Collect(). Even then, finalizers are not executed synchronously with collection. Objects with finalizers are placed on the finalization queue, and the finalizer thread processes them asynchronously.
This nondeterminism means you cannot rely on a finalizer to release a resource at a specific point in your program. For example, a file handle may remain open long after the object goes out of scope. In a short-lived application, the finalizer might never run before the process exits. Finalizers are a safety net, not a primary cleanup mechanism.
The Finalization Queue and the Finalizer Thread
When an object with a finalizer is allocated, the GC registers it in the finalization queue. During collection, if the object is unreachable, the GC moves it from the finalization queue to the freachable queue. The finalizer thread then runs the finalizer and removes the object from the queue. Only after that does the memory become eligible for reclaiming.
This process adds overhead. Objects with finalizers require at least two GC collections to be fully reclaimed: one to detect that they are unreachable and move them to the freachable queue, and another to free the memory after the finalizer runs. This can increase memory pressure and prolong object lifetime, especially if many objects have finalizers.
Why Finalizers Are Not a Replacement for IDisposable
Finalizers are not deterministic, so they cannot guarantee timely release of resources. The recommended way to release unmanaged resources is to implement IDisposable and provide a Dispose() method that callers can invoke explicitly. The finalizer then acts as a backup in case the caller forgets to call Dispose().
The canonical pattern is:
public class ResourceHolder : IDisposable
{
private IntPtr _handle;
private bool _disposed;
public ResourceHolder()
{
_handle = AllocateNativeResource();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
// Release managed resources that implement IDisposable.
}
// Release unmanaged resources.
ReleaseNativeResource(_handle);
_disposed = true;
}
~ResourceHolder()
{
Dispose(false);
}
}Here, Dispose() releases the resource immediately and calls GC.SuppressFinalize(this) to prevent the finalizer from running later. The finalizer runs only if Dispose() was never called, and it calls Dispose(false) to release unmanaged resources without touching managed ones. This pattern frees resources promptly in the common case while still providing a safety net.
The Cost of Finalizers: Performance and Memory
Finalizers have a real performance cost. Every object with a finalizer is registered in the finalization queue, which adds memory overhead and forces the GC to track it. When such an object becomes unreachable, it survives the first collection and is moved to the freachable queue, then the finalizer thread runs, and only after a second collection is the memory actually freed. This can increase memory usage and GC time, especially if many objects have finalizers.
If a class does not directly own an unmanaged resource, do not add a finalizer. A finalizer that only touches managed resources cannot help and adds GC overhead. Reserve finalizers for classes that directly own an unmanaged resource, such as a native handle or an unmanaged memory block.
Common Mistakes with C# Destructors
One common mistake is assuming that a finalizer will run when the object goes out of scope. It will not. Another is accessing other managed objects from inside the finalizer. Finalization order is not guaranteed, so another referenced object may already have been finalized. Accessing it can cause exceptions or undefined behavior.
Another mistake is not calling GC.SuppressFinalize in Dispose(). If you forget it, the finalizer still runs after you have explicitly released the resource, which can cause double-release errors if the finalizer tries to release the same resource again.
Finally, keep finalizers short. If a finalizer blocks, it delays other finalizers and can cause the finalization queue to back up, leading to memory pressure and application stalls.
Finalizers and Inheritance
The derived class's finalizer automatically calls the base class's finalizer. The C# compiler generates the call in a finally block, so even if the derived finalizer body throws, the base finalizer still runs. You do not need to write base.Finalize() explicitly.
If you implement the IDisposable pattern, make Dispose(bool) virtual so derived classes can extend the cleanup logic. A derived class should call base.Dispose(disposing) to release base-class resources. This is the standard pattern, but it is easy to get wrong when a derived class adds its own unmanaged resources.
When to Use a Finalizer in Production Code
In practice, finalizers are rarely needed. The main production scenario is when your class directly owns an unmanaged resource and you need a safety net for callers that fail to call Dispose(). Even then, consider using SafeHandle or a wrapper from the Microsoft.Win32.SafeHandles namespace, which already implements the finalization pattern correctly. For example, SafeFileHandle and SafeProcessHandle wrap native handles and handle finalization safely.
Before writing a finalizer, ask whether you can use a SafeHandle subclass or an existing IDisposable wrapper instead. Most unmanaged resources in .NET are already wrapped in managed classes that implement IDisposable. When you do write a finalizer, follow the Dispose(bool) pattern, suppress finalization in Dispose(), and keep the finalizer minimal.
Finalizer Runtime Behavior
The finalizer runs asynchronously under runtime control. The exact thread implementation can vary by runtime, so do not rely on specific thread identity or on exact timing. If you need to wait for pending finalizers during shutdown or in tests, you can call GC.WaitForPendingFinalizers(), but do not use it as a general cleanup mechanism because it can cause deadlocks if a finalizer waits on a lock held by the current thread.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.