StreamReader.close and StreamWriter.close

E

Eran.Yasso

Hi,

In the MSDN the sample doesn't use the close() method. But I know that
in most languages you do need to use the close() method after reading
and writing to a file.

from MSDN:

public static void Main()
{
string path = @"c:\temp\MyTest.txt";

try
{
if (File.Exists(path))
{
File.Delete(path);
}

using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}

using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}",
e.ToString());
}
}

does the "Using" replaces the needing for Close() method?
 
J

Jon Skeet [C# MVP]

In the MSDN the sample doesn't use the close() method. But I know that
in most languages you do need to use the close() method after reading
and writing to a file.

does the "Using" replaces the needing for Close() method?

Yes. "using" is equivalent to a try/finally block which calls Dispose()
in the finally block.
 
O

Ollie Riches

The simple answer to your question is yes the 'using' statement does replace
the need for a 'Close'...

check out the definitions of the using keyword in c#

http://www.c-sharpcorner.com/Upload...Statement11092005065819AM/UsingStatement.aspx

The 2 classes you mention below - StreamReader & StreamWriter both implement
the IDisposable interface and you can use any class that implements this
interface in a 'using' statement.

These particular classes call 'Close' on the underlying stream object when
the Dispose method is called by the 'using' statement.

A 'using' statement is often called a try - catch - finally block, as in the
variable inside the 'using' statement is automatically wrapped into it's own
try - catch - finally block and the Dispose mehtod of the IDisposable
interface is always called in the finally statement.

HTH

Ollie Riches
 
L

Laurent Bugnion [MVP]

Hi,
Yes. "using" is equivalent to a try/finally block which calls Dispose()
in the finally block.

About that, a question: I have this (pseudo)code that I want to refactor
using the "using" clause:

StreamWriter writer = null;

try
{
// ...
}
catch ( Exception ex )
{
logger.Log( ex.Message );
}
finally
{
if ( writer != null )
{
writer.Close();
writer.Dispose();
}
}

How to code that with a "using"? How can I do something special in case
an Exception occurs?

Greetings,
Laurent
 
J

Jon Skeet [C# MVP]

Laurent Bugnion said:
About that, a question: I have this (pseudo)code that I want to refactor
using the "using" clause:

How to code that with a "using"? How can I do something special in case
an Exception occurs?

Either put a try/catch within the using statement, or outside it.
 
L

Laurent Bugnion [MVP]

Hi Jon,
Either put a try/catch within the using statement, or outside it.

Thanks. I thought of that already, just wanted to confirm. Question
though: The code would become something like:

using ( StreamWriter writer = new StreamWriter( ... ) )
{
try
{
// ...
}
catch ( Exception ex )
{
logger.Log( ex.Message );

throw; // Is that correct??
}
}

I think that I shouldn't rethrow the Exception, is that correct? The
"using" clause is a try/finally, not try/catch/finally?

Thanks,
Laurent
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Laurent said:
Hi Jon,


Thanks. I thought of that already, just wanted to confirm. Question
though: The code would become something like:

using ( StreamWriter writer = new StreamWriter( ... ) )
{
try
{
// ...
}
catch ( Exception ex )
{
logger.Log( ex.Message );

throw; // Is that correct??
}
}

I think that I shouldn't rethrow the Exception, is that correct? The
"using" clause is a try/finally, not try/catch/finally?

Thanks,
Laurent

If you rethrow the exception or not depends on if you completely handled
it or not. If the code that called this method needs to know about the
exception, you should rethow it.

I haven't investigated if a using clause has a catch or not, but if it
has, it rethrows the exception. The point is that the using clause does
not consume exceptions.
 
L

Laurent Bugnion [MVP]

Hi,

Göran Andersson said:
If you rethrow the exception or not depends on if you completely handled
it or not. If the code that called this method needs to know about the
exception, you should rethow it.

I haven't investigated if a using clause has a catch or not, but if it
has, it rethrows the exception. The point is that the using clause does
not consume exceptions.

That confirms what I actually observed. Thanks.

Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top