If I have this:
StreamWriter cout = new StreamWriter("test.txt"); cout.WriteLine("XXX");
clr throws an IOException because I do not close 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 the application:
StreamReader cin = new StreamReader("test.txt"); string text = cin.ReadLine();
without closing the cin file can be opened and written to the OS.
However, after reading the source code of StreamReader.cs , I did not find the destructor method (i.e. ~StreamReader(...) ). So, who frees this file if the garbage collector does not call Dispose and there is no finalization method?
source share