Your question regarding whether or not you should always call Dispose
is usually a heated debate. See this blog for an interesting perspective from respected individuals in the .NET community.
Personally, I think Jeffrey Richter's position that calling Dispose
is not mandatory is incredibly weak. He gives two examples to justify his opinion.
In the first example he says calling Dispose
on Windows Forms controls is tedious and unnecessary in mainstream scenarios. However, he fails to mention that Dispose
actually is called automatically by control containers in those mainstream scenarios.
In the second example he states that a developer may incorrectly assume that the instance from IAsyncResult.WaitHandle
should be aggressively disposed without realizing that the property lazily initializes the wait handle resulting in an unnecessary performance penalty. But, the problem with this example is that the IAsyncResult
itself does not adhere to Microsoft's own published guidelines for dealing with IDisposable
objects. That is if a class holds a reference to an IDisposable
type then the class itself should implement IDisposable
. If IAsyncResult
followed that rule then its own Dispose
method could make the decision regarding which of its constituent members needs disposing.
So unless someone has a more compelling argument I am going to stay in the "always call Dispose" camp with the understanding that there are going to be some fringe cases that arise mostly out of poor design choices.
using
construct. – Luc Touraille