Closing System.IO.FileStream

  • Thread starter Thread starter Alex Moskalyuk
  • Start date Start date
A

Alex Moskalyuk

Does calling Close() on a System.IO.FileStream object cause it to go null?
Is this code
MyFile.Close();
...
if (MyFile != null)
Console.WriteLine ("File not closed");

valid?

The documentation mentions Dispose() being called, but does it go null
automatically, or is the reference nulled on the next garbage collection
run?

Thanks
 
No. Objects cannot become null by themselves, as long as there's a
reference to that object from the application.

However, you can explicitly set the object to null after a close which will
qualify it for garbage collection (provided there are no other references).

-vJ
 
Back
Top