Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The using statement and declaration

Reference for the C# using statement and using declaration: automatic Dispose, IDisposable and IAsyncDisposable, await using, disposal order and pitfalls.

The using statement guarantees that an object's Dispose() method is called when you finish with it, even if an exception is thrown. It is C#'s tool for releasing files, sockets, database connections, locks and other resources that the garbage collector does not clean up promptly. It works with any type that implements IDisposable, IAsyncDisposable, or (for ref structs) has an accessible Dispose() method.

There are two forms. The classic using statement has a block, and the resource is disposed at the end of that block. The using declaration (C# 8) has no block: the resource is disposed when the enclosing scope ends, which avoids extra nesting.

C#
using (var reader = new StringReader(text))
{
    Console.WriteLine(reader.ReadLine());
}

using var writer = new StringWriter();   // disposed at the end of the method

Try it

C# Disposal order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Exceptions, several resources and await using

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
using (ResourceType name = expression) statement
using (expression) statement
using ResourceType name = expression;          // declaration
await using (var name = expression) statement  // IAsyncDisposable
await using var name = expression;
Name Description
ResourceType A type implementing IDisposable (or IAsyncDisposable with await using).
expression Creates or returns the resource. null is allowed and is simply skipped.

Errors: using a type with no suitable Dispose gives CS1674. The variable is read-only: assigning to it fails with CS1656. await using outside an async method fails with CS4033.

Usage notes

  • Equivalent code. A using statement expands to try { ... } finally { resource?.Dispose(); }.
  • Scope of a declaration is the enclosing block; disposal happens when control leaves it, in the reverse order of declaration. Add braces { } around a section if you want to dispose earlier.
  • Return carefully: do not return an object that was created by a using and is already disposed. Returning a stream wrapped in a using is a common bug.
  • Double disposal should be harmless: well-written Dispose methods can be called more than once.
  • If both are implemented, await using calls DisposeAsync(), plain using calls Dispose().
  • Exceptions in Dispose replace exceptions in flight in the classic pattern, so keep Dispose from throwing.
  • Do not confuse this with the using directive, which imports namespaces, and with using static / using Alias = Type.
  • Dependency injection: objects resolved from a container are owned (and disposed) by it; do not wrap them in using.