开发者

Should i dispose a stringwriter? or reuse somehow?

开发者 https://www.devze.com 2022-12-23 04:52 出处:网络
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() on the sw a

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() on the sw and allocate a new object? or should i do something like .close() and do something else to empty the buffer?


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.


If you have a certain function that needs an empty StringWriter every time, why not just create a new StringWriter in that function?

But, getting back to your original question, I used .NET Reflector to check out what StringWriter's Dispose does. All it does is call back into the base class' (TextWriter) Dispose -- which does nothing.

So if you really need to "re-use" the StringWriter you're exposing, it looks like it would be safe to just re-create a new instance when you need it (although I think you should reconsider your design of exposing a StringWriter as a public member on a class).


From O'reilly's C# in a nutshell:

There are, however, three scenarios for not disposing:

  • ...
  • ...
  • When an object’s Dispose method is unnecessary by design, and disposing that object would add complexity to your program

The third category includes the following classes: WebClient, StringReader, String Writer, and BackgroundWorker (in System.ComponentModel). These types are disposable under the duress of their base class rather than through a genuine need to perform essential cleanup. If you happen to instantiate and work with such an object entirely in one method, wrapping it in a using block adds little inconvenience. But if the object is longer-lasting, keeping track of when it’s no longer used so that you can dispose of it adds unnecessary complexity. In such cases, you can simply ignore object disposal.

tl;dr: no, you don't need to dispose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号