Do you need to close stream reader in a using block

Asked By Isaac Ortega On

I have a stream reader inside a using block of code in C#. Do I need to call reader. Close as part of this to close off the process and allow GC to clean up any memory or does it happen on its own? Here is the code that I am using.

using (StreamReader reader = new StreamReader(responseStream))
string fileContents = reader.ReadToEnd();

1 Answer

Answered By Chris Evans On

No, there is no need to explicitly call reader.Close if the reading process is inside a using block. By using a using statement (that sounds awkward out loud) everything will be closed and disposed when the code exits the block. It is all handled as part of the i disposable interface that the stream reader implements.

Related Questions


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.