Following this MSDN resource, the code
using System;
class BaseClass : IDisposable
{
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
// Free any other managed objects here.
//
}
// Free any unmanaged objects here.
//
disposed = true;
}
~BaseClass()
{
Dispose(false);
}
}
has a comment about freeing managed resources/objects.
As far as I know, the garbage collector will do that, but is it good practice to actually write such code there, and by such I mean pointing the objects's references to null?
I believe that's the best you can do at "disposing" of a managed resource, although you don't achieve anything in the process, as the garbage collector will do the actual heavy lifting, not you setting their references to null. (The garbage collector will notice that the class itself holding these objects/resources is not used (no references), making this task meaningless)
Thanks!