| title | using statement - ensure the correct use of disposable objects | |
|---|---|---|
| description | Use the C# using statement or declaration to ensure the correct use of disposable objects | |
| ms.date | 01/16/2026 | |
| f1_keywords |
|
|
| helpviewer_keywords |
|
The using statement ensures the correct use of an xref:System.IDisposable instance:
:::code language="csharp" source="snippets/using/Program.cs" id="Using":::
When control leaves the block of the using statement, the 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 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.
[!INCLUDEcsharp-version-note]
A variable declared by the using statement or declaration is readonly. You can't 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 that's accessible, parameterless, and has a void return type.
A return inside a using block still guarantees disposal. The compiler rewrites it into a try/finally, so the resource’s Dispose is always called before the method actually returns.
The using statement can also be of the following form:
using (expression)
{
// ...
}where expression produces a disposable instance. The following example demonstrates that form:
:::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 you should declare a disposable variable within the using statement or with the using declaration.
For more information, see The using statement section of the C# language specification.
- xref:System.IDisposable?displayProperty=nameWithType
- xref:System.IAsyncDisposable?displayProperty=nameWithType
- Using objects that implement IDisposable
- Implement a Dispose method
- Implement a DisposeAsync method
- Use simple 'using' statement (style rule IDE0063)
usingdirective