Dispose vs Dispose(bool)

Dispose(bool) is a pattern to implement Finalize and Dispose to Clean Up Unmanaged Resources , see this for detail


IDisposable provides a method with the signature

public void Dispose()

Microsoft best practices (Implement a Dispose method) recommend making a second private method with the signature

private void Dispose(bool)

Your public Dispose method and finalizer should call this private Dispose method to prevent disposing managed resources multiple times.

You can fix the warning you are getting by either implementing IDisposable and disposing of your font object in the dispose method, or creating a Dispose(bool) method in your class, and make your finalizer call that method.


Dispose(bool) is not meant to be public and that is why you don't see it on Font.

In case some user of your class forgets to call Dispose on your method, you will release the unmanaged resources only by making a call to Dispose(false) in the Finalizer.

In case IDispose is called correctly, you call the Dispose on managed resources and also take care of the unmanaged.

The flag is to distinguish the two cases.

It is a pattern recommended by MSDN.

Tags:

C#

Dispose