Back to results for “Exception Handling”
C# › Exception Handling

When should you use using or await using?

Intermediate

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.