Using keyword and IDisposable

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi all,

I currenty have a datalayer and have decided to impliment IDisposable
with it. The data layer contains a number of objects which I can call
dispose on, but i'm not too sure if you need to worry about strings.
In the dispose method, is it best to set these to "", or set them to
null? Also, I have an List<T> object, should this be cleared and set
to null?
I usually clean things up myself, but figured for a datalayer,
IDisposable and using{} might be an extra safeguard.

Any help with this would be appreciated.

Paul
 
idisposable isn't required unless you have special circumstances for
cleaning up objects. Strings are handled automatically. Setting a string to
empty or null does nothing for you and in fact prolongs the life of the
string by the extra references. If you intend your data layer to collect
unmanaged objects at some point in its lifetime or requires care in
destroying the objects, definitely implement idisposable. I understand some
people simply like to implement idisposable because it is recommended. I
think that's ok too as long as you understand its impact on your code.

--
Regards,
Alvin Bruney

Want a free copy of VS 2008 w/ MSDN premium subscription?
Details athttp://msmvps.com/blogs/alvin/Default.aspx

Auther Plug
OWC Blackbook now on download atwww.lulu.com/owc









- Show quoted text -

Thanks for the reply Alvin. The datalayer seems to do a pretty good
job of closing connections and cleaning up after itself (I haven't had
any issues yet), but recently I read a blog regarding someone
implimenting IDisposable in their DB layer and seeing memory usage
reduced by up to 40% - so thought i'd look into it. Does implementing
it have a performance hit? If so, i'll just stick to the way I was
doing my clean up before.

Thanks for the help.
 
a class implements idisposable if it uses unmanged resources, and wants them
released as soon as possible, not when the GC get around to it. if a class
implements idisposable it should always be called. better yet use a using
statement. database connections, and commands need to be disposed. if your
code (as it should) always disposes these object before returning from any
method, then your class does not need to implement idispose.

there is a cost to calling idispose, as it forces a GC run on the current
thread rather than the background thread.

List<> does not implement IDispose as it does not need it. but if you insert
objects that require disposing, your code needs to loop thru the list and
dispose the objects when you are done with the list<>

-- bruce (sqlwork.com)
 
Back
Top