0

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();
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

1 Answers1

1

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.
}
John
  • 3,057
  • 1
  • 4
  • 10