Difference Between Dispose() and Finalize() in C#
In C#, both Dispose() and Finalize() are used to release
unmanaged resources such as file handles, database connections, or network sockets.
These are resources not handled by the .NET runtime's garbage collector.
Learn more about unmanaged resources on the official Microsoft documentation.
๐งน What is Dispose()?
- Part of the IDisposable interface
- Must be called manually or via a
usingblock - Releases resources deterministically
- Ideal for: Files, database connections, network streams
๐️ What is Finalize()?
- Special method:
~ClassName()(destructor syntax) - Called automatically by the Garbage Collector
- Executes non-deterministically
- Used as a backup if
Dispose()is not called - Warning: Finalize can slow down garbage collection
๐ Key Differences: Dispose() vs Finalize()
| Feature | Dispose() |
Finalize() |
|---|---|---|
| Triggered by | Developer (manual) | Garbage Collector (automatic) |
| Defined in | IDisposable interface |
Destructor syntax (~ClassName()) |
| Execution timing | Deterministic | Non-deterministic |
| Role | Primary cleanup mechanism | Backup mechanism |
| Performance impact | Efficient | Slower due to GC overhead |
| Accessibility | Public | Protected |
๐งช Real Example: Using Dispose() and Finalize() Together
public class FileWriter : IDisposable
{
private StreamWriter _writer;
private bool _disposed = false;
public FileWriter(string path)
{
_writer = new StreamWriter(path);
}
public void Write(string text)
{
if (_disposed)
throw new ObjectDisposedException("FileWriter");
_writer.WriteLine(text);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // Prevent Finalize from running
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_writer?.Dispose(); // Managed resources
}
// Unmanaged cleanup if needed
_disposed = true;
}
}
~FileWriter()
{
Dispose(false);
}
}
✔️ Usage Example:
using (var file = new FileWriter("log.txt"))
{
file.Write("Logging message...");
}
// Dispose is called automatically at the end of the using block
๐ซ Common Mistakes to Avoid
- Forgetting to call
Dispose()or not using ausingblock - Not suppressing finalization with
GC.SuppressFinalizeafter disposing - Not implementing
Dispose(bool disposing)pattern when using finalizers
๐ Summary & Best Practices
- Use
Dispose()for controlled, manual cleanup of unmanaged resources. - Use
Finalize()only when absolutely necessary, and always pair it with theDispose(bool)pattern. - Best Practice: Prefer
usingblocks withIDisposableobjects to ensure timely cleanup. - Design Tip: Always call
GC.SuppressFinalizeto prevent redundant cleanup. - For async cleanup, consider implementing
IAsyncDisposable.

Comments
Post a Comment