I know that dispose
method is used for garbage collecting funtionality.
But in this example, I don't know precisely what dispose
does in Filestream
FileStream fl = new FileStream("file1.txt",FileMode.Open);
fl.Dispose();
I know that dispose
method is used for garbage collecting funtionality.
But in this example, I don't know precisely what dispose
does in Filestream
FileStream fl = new FileStream("file1.txt",FileMode.Open);
fl.Dispose();
Creating a FileStream
opens a file. Disposing the FileStream
closes that file. If you're going to do it explicitly, you ought to call Close
rather than Dispose
, although either is OK. You shouldn't do it explicitly though. You should create the object with a using
statement and then it will be implicitly disposed:
using (var fs = new FileStream("file1.txt", FileMode.Open))
{
// Use fs here.
}