Skip to content

C# Feature - await using

await using disposes resources asynchronously when they implement IAsyncDisposable. It is the async counterpart to using.

This feature was introduced in C# 8.0 (2019).

Some resources need asynchronous cleanup, such as flushing buffers, completing network writes, or closing remote connections. A synchronous Dispose() call cannot express that work well. await using keeps disposal aligned with async control flow.

  • Use it for types that implement IAsyncDisposable.
  • Use it in async methods that work with streams, transactions, writers, or SDK types that expose async cleanup.
  • Prefer it over explicit try/finally plus DisposeAsync() when the scope is straightforward.
var stream = new AsyncFileWriter(path);
try
{
await stream.WriteAsync(payload);
}
finally
{
await stream.DisposeAsync();
}
await using var stream = new AsyncFileWriter(path);
await stream.WriteAsync(payload);
await using var transaction = await connection.BeginTransactionAsync();
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
  • await using only works for IAsyncDisposable; it is not interchangeable with IDisposable.
  • Disposal happens at the end of the current scope, so keep the scope as small as practical.
  • If both sync and async disposal exist, prefer the async form inside async codepaths.
  • C# 8 introduced await using together with async disposal support.