Skip to content

Latest commit

 

History

History
68 lines (45 loc) · 4.07 KB

File metadata and controls

68 lines (45 loc) · 4.07 KB
title description ms.date f1_keywords helpviewer_keywords
using statement - ensure the correct use of disposable objects
Use the C# using statement or declaration to ensure the correct use of disposable objects
03/13/2023
using-statement_CSharpKeyword
using statement [C#]

using statement - ensure the correct use of disposable objects

The using statement ensures the correct use of an xref:System.IDisposable instance:

:::code language="csharp" source="snippets/using/Program.cs" id="Using":::

When the control leaves the block of the using statement, an acquired xref:System.IDisposable instance is disposed. In particular, the using statement ensures that a disposable instance is disposed even if an exception occurs within the block of the using statement. In the preceding example, an opened file is closed after all lines are processed.

Use the await using statement to correctly use an xref:System.IAsyncDisposable instance:

:::code language="csharp" source="snippets/using/Program.cs" id="AwaitUsing":::

For more information about using of xref:System.IAsyncDisposable instances, see the Using async disposable section of the Implement a DisposeAsync method article.

You can also use a using declaration that doesn't require braces:

:::code language="csharp" source="snippets/using/Program.cs" id="UsingDeclaration":::

When declared in a using declaration, a local variable is disposed at the end of the scope in which it's declared. In the preceding example, disposal happens at the end of a method.

A variable declared by the using statement or declaration is readonly. You cannot reassign it or pass it as a ref or out parameter.

You can declare several instances of the same type in one using statement, as the following example shows:

:::code language="csharp" source="snippets/using/Program.cs" id="MultipleResources":::

When you declare several instances in one using statement, they are disposed in reverse order of declaration.

You can also use the using statement and declaration with an instance of a ref struct that fits the disposable pattern. That is, it has an instance Dispose method, which is accessible, parameterless and has a void return type.

The using statement can also be of the following form:

using (expression)
{
    // ...
}

where expression produces a disposable instance. The following example demonstrates that:

:::code language="csharp" source="snippets/using/Program.cs" id="UsingWithExpression":::

Warning

In the preceding example, after control leaves the using statement, a disposable instance remains in scope while it's already disposed. If you use that instance further, you might encounter an exception, for example, xref:System.ObjectDisposedException. That's why we recommend declaring a disposable variable within the using statement or with the using declaration.

C# language specification

For more information, see The using statement section of the C# language specification and the proposal note about "pattern-based using" and "using declarations".

See also