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)
Before
Section titled “Before”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.