If I have this:
StreamWriter cout = new StreamWriter("test.txt");
cout.WriteLine("XXX");
// here IOException...
StreamReader cin = new StreamReader("test.txt");
string text = cin.ReadLine();
the clr throws an IOException
because I haven't close the cout
.
In fact If I do this:
StreamWriter cout = new StreamWriter("test.txt");
cout.WriteLine("XXX");
cout.Close();
StreamReader cin = new StreamReader("test.txt");
string text = cin.ReadLine();
I have no exception.
But If I do this and then exit from the application:
StreamReader cin = new StreamReader("test.txt");
string text = cin.ReadLine();
without closing cin
the file can from the OS opened and written.
However reading the source code of StreamReader.cs
I didn't' find a destructor method (i.e. ~StreamReader(...)
). So who does free that file if the garbage collector doesn't invoke Dispose
and there is no finalization method?