I have been looking at the standard Dispose pattern and I'm just wondering what I need to write to free managed resources? If these resources are 'managed' already then surely I shouldn't need to do anything.
If that's the case, and my class doesn't hold any unmanaged resources (hence no need for it to be finalized by GC) then do I only need to suppress finalization in my Dispose method? :-
public void Dispose()
{
GC.SuppressFinalize(this);
}
so suppose this is my class:
public sealed class MyClass : IDisposable
{
IList<MyObject> objects; // MyObject doesn't hold any unmanaged resource
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
// do I need to set the list to null and
// call Dispose on each item in the list?
if (disposing)
{
foreach (var o in objects)
o.Dispose();
objects = null;
}
}
_disposed = true;
}
~MyClass()
{
Dispose(false);
}
}
Do I actually need to free the managed resources here?
Thanks,