C# Feature - Using declarations
Overview
Section titled βOverviewβUsing declarations shorten deterministic disposal by letting the scope, not a nested block, define the disposal boundary.
Introduced In
Section titled βIntroduced InβC# 8.0 (2019)
using (var stream = File.OpenRead(path)){ Process(stream);}using var stream = File.OpenRead(path);Process(stream);Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Disposal happens at the end of the containing scope, not right after the next statement.
- Use the classic
usingstatement when you need a tighter disposal block.