Should I have a line comment? or reuse?

I will make it simple. I have a stringwriter as a member of a class, so I cannot use using (). I want an empty sw everytime I call a specific function. Should I call Dispose () in sw and select a new object? or should I do something like .close () and do something else to clear the buffer?

+7
source share
3 answers

I agree with another answering machine that you should look at your design to see if the recording method is really line level.

However, I do not agree with the point of view

"All he does is call back to the base class' (TextWriter) Dispose - which does nothing."

When using classes, if the type implements Dispose , you really have to call it, explicitly or using "use".

In this case, it is true that StringWriter.Dispose calls up to TextWriter.Dispose , which does nothing, but I really do not advise "just ignore it."

The whole nature and polymorphism is that this type can be replaced by a derived type.

Today, StringWriter in your code can be replaced tomorrow with EvenBetterStringWriter , which may have a more meaningful implementation for Dispose .

If it implements Dispose and you use it, you should seriously consider making a call when you are done.

Having privileged knowledge about the insides of this particular concrete implementation is dangerous when you allow it to be guided by your design like that. The author of the StringWriter class StringWriter clearly intended to call Dispose, or it would not be there.

+11
source share

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

But returning to the original question, I used the .NET Reflector to check what StringWriter Dispose does. All he does is call back to the Dispose base class (TextWriter) - which does nothing.

So, if you really need to β€œreuse” the created StringWriter, it would seem safe to just re-create a new instance when you need it (although I think you should redefine your design by exposing StringWriter as an open member of the class).

+5
source share

From O'reilly C # in a nutshell:

However, there are three scenarios for non-disposition:

  • ...
  • ...
  • If the Dispose object is not needed by design, and disposing of this object will 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 coercion of their base class, and not through the real need to carry out the necessary cleaning. If you have to create an instance and work with such an object completely in one method, wrapping it in the add block adds a bit of inconvenience. But if the object is longer, keeping track of when it is no longer being used so that you can get rid of it. excessive complexity. In such cases, you can simply ignore the delete object.

tl; dr: no, you do not need to dispose.

0
source share

All Articles