While Dispose Pattern is widely discussed on the Internet and SOF I could not find any resource that answer my questions.
Therefore, please be kind to read to the end before marking this as a duplicate. I will happily remove the question if you could point me to a previous question that I have missed.
I have seen many classes that (blindly?) implement dispose pattern without implementing a finalizer.
The MSDN article on Dispose Pattern says:
The Boolean parameter disposing indicates whether the method was invoked from the IDisposable.Dispose implementation or from the finalizer. The Dispose(bool) implementation should check the parameter before accessing other reference objects (e.g., the resource field in the preceding sample). Such objects should only be accessed when the method is called from the IDisposable.Dispose implementation (when the disposing parameter is equal to true). If the method is invoked from the finalizer (disposing is false), other objects should not be accessed. The reason is that objects are finalized in an unpredictable order and so they, or any of their dependencies, might already have been finalized.
According to what I understand GC calls the finalizer method (if implemented) which in turn should call the Dispose(bool) method with parameter as false.
My First question is if a class does not implement a finalizer then does the Dispose(bool) ever get called with parameter as false? (e.g. by something in the CLR that I haven't come across)
I understand Dispose(bool) can be used to make sure that objects are only disposed once.
Therefore, my second question is if a class does not need to implement a finalizer then can it simply implement the Dispose method like below?
private bool objectDisposed;
public void Dispose()
{
if (!objectDisposed)
{
// Release/dispose managed resources
// ...
// ...
objectDisposed = true;
}
}