You are right, in a class that follows the guidelines put forth by MS
for implementing IDisposable, if you do not call Dispose, the GC will
ultimately dispose of the classes.
However, the class implements IDisposable as an indicator to let you
know that you should take the lifetime of the instance into account, and
call Dispose on it when you are done with it, usually because the resource
is valuable. Things like socket connections, database connections, file
handles and the like all are great candidates for object wrappers with
IDisposable implementations.
So if you have a member field which is an IDisposable instance, and it
lives for the life of your object (as opposed to a specific lifetime defined
by accessing methods/properties on your class), then you should implement
IDisposable, and when using the class, call Dispose when done with the
instance.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
<cg*********@hotmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
For any class I write in C# that has a member variable that implements
IDisposable my class implements the IDisposable pattern. I do this to
guarantee the reference to the member is explicitly released and the
object is eligible for garbage collection when my class is disposed or
its' finalizer is called by GC.
My question is whether using this approach is even necessary? My
feeling is yes because it ensures the member is explicitly Disposed
when my Class is, but I'm not certain it provides any real benefit
since the GC will release the reference when it eventually gets to it.
Thanks for the input.