Michael and Dennis,
So again, we get back to someone's recommendations somewhere; presumably
the class developer's recommendation.
Unfortunately that may be the "Best" you are going to get. I normally read
the documentation on the class to see if Dispose is "required". If I'm using
a lot of the objects of a Disposable type & I'm in doubt I normally call
Dispose. If I have only one or two instances & I'm in doubt I may not call
it, as waiting for Finalize may not be that big a deal. Of course if
profiling (too many objects waiting to be finalized by the GC) or other
system problems (connection pool running dry) indicates that I should be
calling Dispose then I will...
To see "exactly" what Dispose does, at least what its intent is see:
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp
Dispose & Finalize are used together to provide an explicit (Dispose) and
implicit (Finalize) method of releasing unmanaged resources an object may be
directly using. If the class has unmanaged resources then Finalize will
clean them up. Dispose allows you to do this clean up early & suppress the
finalization. By suppressing the finalization you enable the garbage
collector to run more efficiently. You can use tools such as the CLR
profiler & PerfMon to find out how many objects are
As JD has suggested Dispose can do one of three things:
1) nothing
2) cleans up unmanaged resources
3) cleans up managed resources
I use #3 in my classes when I have a class that contains (Has a) #2 class.
#1 classes are common in the DataSet object model & classes that inherit
from System.ComponentModel.Component. NOTE: a number of classes that inherit
from Component are #2 or #3 classes! For example most controls
(System.Windows.Forms.Control) are #2 classes as they contain a Win32 window
handle.
Now IMHO there are 3 schools of thought on calling Dispose:
1) Never call Dispose, instead let the Finalizer take care of unmanaged
resources. This is probably the worst & sloppiest attitude you can take on
calling Dispose. Profiling & various exceptions will indicate problems when
using this method.
2) Always call Dispose, the "better safe then sorry" pattern.
3) "Appropriately" call Dispose. Unfortunately this is the hardest one to
achieve, as you need to learn from experience, others, or profiling &
various exceptions... It is the one I strife for...
Info on the CLR Profiler:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto13.asp
http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp
FWIW: VB.NET 2005 (.net 2.0 aka Whidbey, due out later in 2005) will include
a Using statement that will simplify calling Dispose for you.
http://msdn2.microsoft.com/library/htd05whh.aspx
Using thePen As New Pen(...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
gr.DrawLine(thePen, ...)
End Using
Hope this helps
Jay