using Statement

noun

say it: “YOO-zing STAYT-ment” /ˈjuːzɪŋ ˈsteɪtmənt/

A C# statement that closes or frees an object, such as a database connection, as soon as the code is done with it.

What does using Statement do in C#?

using Statement — A statement that disposes a resource automatically when execution leaves its scope.

class OrderFile
{
    string Read(string path)
    {
        using StreamReader reader = new(path);
        return reader.ReadToEnd();
    }
}

In a sentence

  • “The using statement closes the connection even if the query throws an error.”

Level: Journeyman · More C# terms at this level

Updated