How critical is it to call Dispose() of a class which implements
IDisposable(). Though it may be recommended as a good practise, which
situations make it very important ?
1. It is playing by the rules, so you should do it. Failing to dispose may
work today but fail tomorrow after a .net upgrade without any changes to your
code. Honor your oop contracts and genuflect often and peace shall be upon
you.
2. Some (fxcop, purists) say always dispose your IDisposables. Hmmm.
There are classes that implement IDisposable that also provide a documented
alternative. For example, WebResponse provides the Close method, so I Close
and I do not Dispose just like the MS provided samples and documentation
indicate. That seems reasonable provided you don't go overboard with
contract waivers.
3. I go overboard. My general approach to .net development is to follow
the examples and add dispose when I get into trouble. I detest the concept
and implementation of IDisposable. I feel better now.
4. Dispose is least important at app shutdown. When I shut down my apps, I
don't worry about disposing unless I know about an open file or something
like that.
5. Dispose is most important when you instantiate-destroy repeatedly and
dispose is actually necessary to prevent a memory leak. For example,
System.Windows.Forms.MainMenu
will leak if you instantiate MainMenu repeatedly and fail to dispose.
Sorry, I don't have a list of classes that have this behavior. Even if I
did, it may be valid today and not valid tomorrow, so you should always
dispose (or equivalent) your IDisposables. Amen.