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/UploadF...Statement.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
<Er********@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
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?