---
title: "What is the difference between Dispose() and Finalize()?"  
description: "What is the difference between Dispose() and Finalize()?"  
author: "Anubhav Sharma"  
published: 2025-06-19  
updated: 2025-06-24  
canonical: https://www.mindstick.com/forum/161732/what-is-the-difference-between-dispose-and-finalize  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# What is the difference between Dispose() and Finalize()?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between `Dispose()` and `Finalize()`?

## Replies

### Reply by Ponu Maurya

In C#, `Dispose()` and `Finalize()` are both used to **release unmanaged resources** (like file handles, database connections, etc.), but they are used in **different ways** and serve **different purposes**.

## `Dispose()` (IDisposable Interface)

- **Explicit cleanup** method.
- Called **manually** by the developer to release unmanaged resources.
- Belongs to the `IDisposable` interface.
- Often used in a `using` block, which automatically calls `Dispose()`.

```cs
public class Resource : IDisposable
{
    private FileStream _file;

    public Resource(string path)
    {
        _file = new FileStream(path, FileMode.Open);
    }

    public void Dispose()
    {
        _file?.Dispose();  // Free unmanaged resource
        GC.SuppressFinalize(this); // Optional: tells GC not to call Finalize
    }
}
```

**Use when**: You want to explicitly and deterministically release resources.

## `Finalize()` (Destructor)

- Called by the **Garbage Collector (GC)** when the object is no longer in use.
- Cannot be called directly — handled automatically by the CLR.
- Used as a **safety net** in case `Dispose()` wasn’t called.
- Implemented via a **destructor** (`~ClassName()` in C#).

```cs
public class Resource
{
    ~Resource()
    {
        // Cleanup logic
    }
}
```

## Drawbacks:

- Runs on GC, so **timing is non-deterministic**.
- Expensive: keeps object in memory longer (until finalization).

## Best Practice: Use `Dispose()` and Suppress Finalize

```cs
public class Resource : IDisposable
{
    private bool _disposed = false;

    ~Resource()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this); // Skip finalizer
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                // Dispose managed resources
            }
            // Dispose unmanaged resources

            _disposed = true;
        }
    }
}
```

## Summary Table

| Feature | `Dispose()` | `Finalize()` (Destructor) |
| --- | --- | --- |
| Called by | Developer (explicit) | GC (implicit) |
| Timing | Deterministic | Non-deterministic |
| Interface | Implements `IDisposable` | Implicit via `~ClassName()` |
| Performance | Efficient | Slower (GC + finalizer queue) |
| Use case | Close files, DB, streams | Safety net for unmanaged cleanup |


---

Original Source: https://www.mindstick.com/forum/161732/what-is-the-difference-between-dispose-and-finalize

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
