Home All Groups Group Topic Archive Search About

Proper resource deallocation through Dispose()

Author
12 Jul 2006 3:49 PM
Michael Russell
Hi all,
I'm fairly new to C#, coming in from several years with C++.  So, I'm
trying to get a handle on .Net's implementation (and the proper use of)
Garbage Collection.

I have a small database app mostly written, but am a little fuzzy on
when to Dispose.  If I understand things correctly, then eventually the
GC in the Framework will always clean up for me (call Finalize).  It
will not, however, close resources manually opened (Db connections,
files, etc).  The docs also say "Always call Dispose before you release
your last reference to the Component", I assume also referring to
anything that implements IDisposable.

Having said that, I created a class with instance variables of types
DbDataSet, DbDataAdapter, DbCommand, with methods like "LoadSet",
"AddRecord", etc, to encapsulate the data work.  Now, since I have
instance variables that are unmanaged (implement IDisposable), I must
also implement IDisposable in my class to make sure that Dispose() is
called on all my variables.  Likewise, if my class is used for an
instance variable, that parent class must also implement IDisposable.

Am I interpreting this correctly?

Thanks,
Michael

AddThis Social Bookmark Button