C# – Dispose() for cleaning up managed resources

cdisposefinalize

In this answer I found,

Cleanup the unmanaged resources in the Finalize method and the
managed ones in the Dispose method, when the Dispose/Finalize pattern
has been used in your code.

And later I found this nice article about finalize and dispose and got a clear idea about them. The article has the following code(Page 3), to explain the concepts:

class Test : IDisposable
{
    private bool isDisposed = false;

    ~Test()
    {
       Dispose(false);
    }

    protected void Dispose(bool disposing)
    {
       if (disposing)
       {
          // Code to dispose the managed resources of the class
       }
       // Code to dispose the un-managed resources of the class

       isDisposed = true;
    }

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

But below that, the same note (which I included in the beginning of this question) appears.

The Dispose/Finalize Pattern
Microsoft recommends that you implement both Dispose and Finalize when working with unmanaged resources. The correct sequence then would
be for a developer to call Dispose. The Finalize implementation would
run and the resources would still be released when the object is
garbage collected even if a developer neglected to call the Dispose
method explicitly. Francesco Balena writes in his blog "the
Dispose/Finalize pattern should be used only when your type invokes
unmanaged code that allocates unmanaged resources (including unmanaged
memory) and returns a handle that you must use eventually to release
the resource. Both dispose and finalize must chain up to their parent
objects by calling their parent's respective methods after they have
disposed or finalized their own members".
Simply put, cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the
Dispose/Finalize pattern has been used in your code.

Now I am confused again. In the entire article and in the code sample, it is shown that unmanaged resources should be freed in Dispose(). But then what is the relevance of that comment?

Edit:

As it is confirmed that this line :

Simply put, cleanup the unmanaged resources in the Finalize method and
the managed ones in the Dispose method, when the Dispose/Finalize
pattern has been used in your code

is erroneous, I edited this answer.

Best Answer

See its very simple.

  1. If you are dealing with unmanaged resources - Implement both Dispose and Finalize. Dispose is to be called by developers to free up the resources as soon as they see it that its no longer needed for them. If they forget to call Dispose then Framework calls the finalize in its own GC cycle (usually will take its own sweet time).
  2. If your object uses disposable objects internally - You implement Dispose() if you created and retained a reference to any object of a type which implements Dispose() and which you haven't already disposed.
  3. If neither of the above is the case (you are NOT dealing with unmanaged resources nor your object uses disposable objects internally) - Then don't do anything. Don't implement Finalize nor Dispose.

Some classic examples:

System.IO.FileStream object manages the lock/stream handles to files. So it implements both dispose and finalize. If the developer disposes it then the other program can access it right away. If he forgets to dispose it then Framework finalize it and close the handles later in its GC cycle.

System.Text.StringBuilder dose not have any unmanaged resource. So no dispose no finalize.

As far as the pattern is concerned what it means to

// Code to dispose the managed resources of the class

is that call the Dispose methods of any .NET objects that you have as components inside that class

And

// Code to dispose the un-managed resources of the class

Means to close the raw handles and pointers. Here is your updated code with examples

class Test : IDisposable
{
  private bool isDisposed = false;

  ~Test()
  {
    Dispose(false);
  }

  protected void Dispose(bool disposing)
  {
    if (!isDisposed)
    {
      if (disposing)
      {
        // Code to dispose the managed resources of the class
        internalComponent1.Dispose();
        internalComponent2.Dispose();
      }

      // Code to dispose the un-managed resources of the class
      CloseHandle(handle);
      handle = IntPtr.Zero;   

      isDisposed = true;
    }
  }

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

Here is an old question explaining it