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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top