Tag Archives: using

C# using Statement

The using statement provides a convenient way to automatically call dispose on objects that implement the IDisposable interface once they go out of scope.

CODE

using (StreamReader sr = new StreamReader("SomeFile.txt"))
{
    string text = sr.ReadLine();
}

NOTES

The using statement ensures that dispose gets called even if an exception gets thrown.