using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
using 这句话是什么意思,在这里用using有什么作用?
你可去掉 StreamReader sr = new StreamReader("TestFile.txt") String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
{***}相当于
try
{StreamReader sr = new StreamReader("TestFile.txt");***}
finally
{sr.disopose();}保证在任何情况下,sr都能被自动释放~
StreamReader sr = new StreamReader("TestFile.txt");
try
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
finally
{
if (sr != null) ((IDisposable)sr).Dispose();
}
}
using(){}在
{}中使用()中的东西,{}结束后销毁。