Unclear Dispose Timing Relative to Await in Pre‑C# 8 Async Methods
0 reputation · 05 May 2021, 10:57 UTC
The `using` statement is compiled into a `try { … } finally { obj.Dispose(); }` construct, guaranteeing disposal when the block exits.
In an async method, the `await` keyword can suspend execution at any intermediate point, but the exact moment `Dispose` runs relative to that suspension remains unspecified for pre‑C# 8 code.
This uncertainty impacts deterministic resource release and may cause leaks if the awaited operation holds a reference after disposal. Does `Dispose` execute before the awaited continuation resumes? Is disposal guaranteed to occur prior to the suspension point?