Skip to content

IDisposable

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision

If a type implements IDisposable you can use a using scope to automatically dispose the resource after leaving the scope.

Example without using.

FileInfo fh = new FileInfo("somefile.txt");
FileStream fs = fh.OpenRead();
// Do stuff.
fs.Close();

Example with using.

FileInfo fh = new FileInfo("somefile.txt");
using (FileStream fs = fh.OpenRead())
{
  // Do stuff.
}
Clone this wiki locally