views:

633

answers:

4

Does anyone have a good articles or tutorial on correctly using dispose and IDisposable. I am trying to explain this to some junior dev and wanted to get some extra material, examples, etc.

+1  A: 

Many older sources of advice on this imply that IDisposable is linked in some way to finalizers. Pay very close attention to the comments of Herb Sutter on this subject. Do NOT leap to the conclusion that you need to write a finalizer if you implement IDisposable, to "make sure that Dispose runs eventually in case the client forgets to call it". This is generally not the case. A class may implement IDisposable so that it can make various calls on managed objects that it owns, e.g. Dispose those objects in turn. This is absolutely pointless to do from a finalizer because finalizers can't reliably access other managed objects. Also anything a finalizer does must be capable of being called from any thread. Finalizers are a very obscure advanced thing, whereas IDisposable is a simple pattern that ought to be ubiquitous. And the advent of SafeHandle changes the situation further.

Daniel Earwicker
A: 

Here's my one. :-)

http://www.blackwasp.co.uk/IDisposable.aspx

BlackWasp
"If you create a class that does not use unmanaged resources then you should not implement IDisposable."Where do people get that idea from?
Daniel Earwicker
A: 

Tell them to always use the using statement on disposable objects.

using (MyDisposable obj = new MyDisposable())
{
  obj.some_stuff();

} //obj is disposed here
NotDan