Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I call Close() or Dispose() for stream objects?

Classes such as Stream, StreamReader, StreamWriter etc implements IDisposable interface. That means, we can call Dispose() method on objects of these classes. They've also defined a public method called Close(). Now that confuses me, as to what should I call once I'm done with objects? What if I call both?

My current code is this:

using (Stream responseStream = response.GetResponseStream()) {    using (StreamReader reader = new StreamReader(responseStream))    {       using (StreamWriter writer = new StreamWriter(filename))       {          int chunkSize = 1024;          while (!reader.EndOfStream)          {             char[] buffer = new char[chunkSize];             int count = reader.Read(buffer, 0, chunkSize);             if (count != 0)             {                writer.Write(buffer, 0, count);             }          }          writer.Close();       }       reader.Close();    } } 

As you see, I've written using() constructs, which automatically call Dispose() method on each object. But I also call Close() methods. Is it right?

Please suggest me the best practices when using stream objects. :-)

MSDN example doesn't use using() constructs, and call Close() method:

  • How to: Download Files with FTP

Is it good?

like image 365
Nawaz Avatar asked Sep 23 '11 06:09

Nawaz


People also ask

Does Dispose call close?

To do it right, you just need to know that Dispose() calls Close() (a pretty intimate piece of implementation trivia). Further, in the Dispose(bool) cases, you need to ignore Dispose() and just write a Dispose(bool) implementation that makes sure to chain the base class method.

Do I need to Dispose memory stream?

You needn't call either Close or Dispose . MemoryStream doesn't hold any unmanaged resources, so the only resource to be reclaimed is memory. The memory will be reclaimed during garbage collection with the rest of the MemoryStream object when your code no longer references the MemoryStream .

What is the difference between close and Dispose in VB net?

The main difference between Close and Dispose in the case of SqlConnectionObject is: An application can call Close more than one time. No exception is generated. If you called Dispose method SqlConnection object state will be reset.

What is stream close?

Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed. public: virtual void Close();


2 Answers

A quick jump into Reflector.NET shows that the Close() method on StreamWriter is:

public override void Close() {     this.Dispose(true);     GC.SuppressFinalize(this); } 

And StreamReader is:

public override void Close() {     this.Dispose(true); } 

The Dispose(bool disposing) override in StreamReader is:

protected override void Dispose(bool disposing) {     try     {         if ((this.Closable && disposing) && (this.stream != null))         {             this.stream.Close();         }     }     finally     {         if (this.Closable && (this.stream != null))         {             this.stream = null;             /* deleted for brevity */             base.Dispose(disposing);         }     } } 

The StreamWriter method is similar.

So, reading the code it is clear that that you can call Close() & Dispose() on streams as often as you like and in any order. It won't change the behaviour in any way.

So it comes down to whether or not it is more readable to use Dispose(), Close() and/or using ( ... ) { ... }.

My personal preference is that using ( ... ) { ... } should always be used when possible as it helps you to "not run with scissors".

But, while this helps correctness, it does reduce readability. In C# we already have plethora of closing curly braces so how do we know which one actually performs the close on the stream?

So I think it is best to do this:

using (var stream = ...) {     /* code */      stream.Close(); } 

It doesn't affect the behaviour of the code, but it does aid readability.

like image 140
Enigmativity Avatar answered Sep 23 '22 06:09

Enigmativity


No, you shouldn't call those methods manually. At the end of the using block the Dispose() method is automatically called which will take care to free unmanaged resources (at least for standard .NET BCL classes such as streams, readers/writers, ...). So you could also write your code like this:

using (Stream responseStream = response.GetResponseStream())     using (StreamReader reader = new StreamReader(responseStream))         using (StreamWriter writer = new StreamWriter(filename))         {             int chunkSize = 1024;             while (!reader.EndOfStream)             {                  char[] buffer = new char[chunkSize];                  int count = reader.Read(buffer, 0, chunkSize);                  if (count != 0)                  {                      writer.Write(buffer, 0, count);                  }             }          } 

The Close() method calls Dispose().

like image 29
Darin Dimitrov Avatar answered Sep 25 '22 06:09

Darin Dimitrov