I\'ll make it simple. I have a stringwriter as a class member therefore i cannot use using(). I want an empty sw everytime i call a certain function. Should i call Dispose()
I agree with the other answerer, that you should look at your design to see if having the class level string writer is really the way to go.
However, I don't agree with the point about
"All it does is call back into the base class' (TextWriter) Dispose -- which does nothing."
When consuming classes, if a type implements Dispose
, you really should call it, either explicitly or with a "using".
In this case, it is true that StringWriter.Dispose
calls down to TextWriter.Dispose
which does nothing, but I really wouldn't advise "just ignoring it".
The whole point of inheritance and polymorphism is that this type can be swapped out for a derived type.
Today's StringWriter
in your code, may be swapped for tomorrow's EvenBetterStringWriter
which may have a more meaningful implementation for Dispose
.
If it implements Dispose
, and you use it, you should seriously think about calling dispose when you are done.
Having privileged knowledge of the internals of this specific concrete implementation is dangerous when you let it guide your design like this. The author of the StringWriter
class clearly meant for Dispose to be called, or it wouldn't be there.