If you use the using method instead of lets say FileStream.Close();, will the class dispose correctly?
private static string GetString()
{
using(FileStream fs = new FileStream("path", FileMode.Open))
using(StreamReader sr = new StreamReader(fs))
{
return sr.ReadToEnd();
}
}
is equivalent to:
private static string GetString()
{
string toReturn = "";
FileStream fs = new FileStream("path", FileMode.Open)
StreamReader sr = new StreamReader(fs)
toReturn = sr.ReadToEnd();
sr.Close();
fs.Close();
return toReturn;
}
or to:
private static string GetString()
{
FileStream fs;
StreamReader sr;
try
{
string toReturn = "";
fs = new FileStream("path", FileMode.Open)
sr = new StreamReader(fs)
toReturn = sr.ReadToEnd();
sr.Close();
fs.Close();
return toReturn;
}
finally
{
if(sr != null)
sr.Close();
if(fs != null)
fs.Close();
}
}
The code generated from a
usingstatement is very similar to your second example (the biggest difference being that it callsIDisposable.Disposeinstead ofClose). It will always properly dispose of the objects, whether the method exits through areturnor a thrown exception.In case you’re curious, this is the C# code without
usings that compiles to the same IL as your example withusings: