C# Feature - await using
await using
Section titled βawait usingβawait using disposes resources asynchronously when they implement IAsyncDisposable. It is the async counterpart to using.
Introduced In
Section titled βIntroduced InβThis feature was introduced in C# 8.0 (2019).
Why This Feature Exists
Section titled βWhy This Feature Existsβ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.
When To Use It
Section titled βWhen To Use Itβ- 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/finallyplusDisposeAsync()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);Practical Example
Section titled βPractical Exampleβawait using var transaction = await connection.BeginTransactionAsync();
await command.ExecuteNonQueryAsync();await transaction.CommitAsync();Common Pitfalls
Section titled βCommon Pitfallsβawait usingonly works forIAsyncDisposable; it is not interchangeable withIDisposable.- 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.
Version Notes
Section titled βVersion Notesβ- C# 8 introduced
await usingtogether with async disposal support.