Advertisement
Intermediate C#, Exception Handling

When should you use using or await using?

Short answer

For IDisposable/IAsyncDisposable resources — they call Dispose/DisposeAsync deterministically at the end of scope, even on exceptions, replacing manual try/finally.

Save C# mock interview
Advertisement
AModel answer
For IDisposable/IAsyncDisposable resources — they call Dispose/DisposeAsync deterministically at the end of scope, even on exceptions, replacing manual try/finally.
await using var conn = new SqlConnection(cs);
await conn.OpenAsync();
Real-world example
Guaranteeing a stream or connection is released promptly rather than waiting for GC.