I've never understood much of the theory either however I have had some
bad experiences with not implementing IDisposable and adding the bellow
mentioned code to all my classes. That is, the GC does everything when
it want's to ie when it has time to do it - it is not top priority. That
means that unless you specifically dispose/destruct an object, it will
remain in memory for at least some time. That fact alone caused me grief
when my project later used that value when it shouldn't have. Again - it
was my fault it was there but I just assumed the GC would have disposed
it already.
The argument has been put that the GC will do it and Finalizers (are
they the same as Destructors - I don't think so) handle it for you so
why even bother. Ok - why bother even having Finalizers and Destructors
if the GC will do it. I chose the opposite - your code is the most
important thing now-a-days, not disk space or memory usage, so therefor
what have you got to lose by properly implementing a Dispose method.
Using users (using()) is far preferable than tcf in my opinion. I
understand that using dispose within a tcf block adds the dispose call
to the GC stack. In other words, there will be a time delay in having it
really destructed (really disposed). using() on the other hand
apparently calls the destructor immediately.
In a VS scenario, the Dispose is included in the designer.cs code page
because each control created by the designer is placed into a container
control. When the form is closed, the container is disposed of by
calling the dispose method and that's why putting the dispose method in
the designer.cs file makes sense. However, I move this code to my .cs
file if I need to specifically dispose of an object in my code ( this is
rare nowadays because I use using() almost all the time.)
As a matter of course, that is whenever I create a class I add the
following at the end:
#region Dispose
/// <summary>
/// Performs application-defined tasks associated with freeing,
releasing, or resetting unmanaged resources.
/// </summary>
///
~*****() //<---- This is the Destructor ... Put the name of you
Class where the ***** appears.
// The tilde ~ is the actual destructor indicator
{
// In case the client forgets to call
// Dispose , destructor will be invoked for
Dispose(false);
}
public void Dispose()
{
Dispose(true);
// Ensure that the destructor is not called
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed
and unmanaged resources; <c>false</c> to release only unmanaged
resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Free managed objects.
}
// Free unmanaged objects
// Free your own state (unmanaged objects).
// Set large fields to null.
}
#endregion
This works for me and I'm most lilely not going to change anything but
if someone wants to tell me what damage Idoing to my computer I'm happy
to listen :-)
Glen.