Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close or dispose

Tags:

c#

.net

StreamReader class has both close and dispose method. I want to know which method to call to clean up all resources.

If making use of using block, I think it will call its dispose method. Will it be enough to clean up all the resources.

like image 619
user496949 Avatar asked Nov 11 '10 10:11

user496949


People also ask

What is the difference between Dispose and close?

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.

Does Dispose call close?

All replies. Close() will remove the connection from the database. Dispose() removes the connection from the database and calls the internal cleanup of the object to remove reference to all of its variables. Dispose() removes the connection from the database and calls the internal cleanup...

When should you call Dispose?

There is a simple guideline: if you have finished with an object whose type implements IDisposable then call Dispose() on it; you should use a using block to do this.

What is a Dispose method?

Dispose improves performance and optimizes memory by releasing unmanageable objects and scarce resources, like Graphics Device Interface (GDI) handles used in applications with restricted Windows space. The Dispose method, provided by the IDisposable interface, implements Dispose calls.


2 Answers

The using block will call Dispose() on the StreamReader instance. Generally speaking, if a type is IDisposable, you should put it in using scope.

EDIT: If you look at the Close() implementation of StreamReader using Reflector, you will see that it's calling Dispose(true). So if you're not using the using scope, calling Close() manually would be the same as calling Dispose() in this particular case.

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;             this.encoding = null;             this.decoder = null;             this.byteBuffer = null;             this.charBuffer = null;             this.charPos = 0;             this.charLen = 0;             base.Dispose(disposing);         }     } } 
like image 52
Igal Tabachnik Avatar answered Sep 19 '22 21:09

Igal Tabachnik


We all know System.IO.StreamReader is not the only .NET 4.0+ class that implements IDisposable and a Close() method. For the case of StreamReader in this question, the source code shows that the base class TextReader.Close(), TextReader.Dispose() both run the same lines of code. You can also see in the code that TextReader.Dispose() is the implementation for when you call StreamReader.Dispose() (because StreamReader doesn't override that method overload signature of Dispose).

So a call to StreamReader.Dispose() will run this inherited line of code, which calls the protected override method StreamReader.Dispose(disposing: true) and so will StreamReader.Close() call StreamReader.Dispose(disposing: true). So for the case of StreamReader, Close() and Dispose() do happen to run the same lines of code.

A more general, non-class-specific answer to the question of Close() or Dispose()?, might be to note that Microsoft has fairly clear documentation on implementing IDisposable and the Dispose pattern. A quick read is enough to show you that implementing a Close() method is not a requirement of the Dispose pattern.

imho the reason for finding the method Close() on so many classes that implement IDisposable, is the result of convention, not requirement.

Someone commented

Close and Dispose - which to call?

An example of another class that implements IDisposable with the Dispose pattern, and has a Close() method. Does Close() run the same code as Dispose() in this case? I haven't looked at the source code, but I'd say not necessarily.

like image 40
T. Webster Avatar answered Sep 23 '22 21:09

T. Webster