Skip to content

C# Feature - Using declarations

Using declarations shorten deterministic disposal by letting the scope, not a nested block, define the disposal boundary.

C# 8.0 (2019)

using (var stream = File.OpenRead(path))
{
Process(stream);
}
using var stream = File.OpenRead(path);
Process(stream);
  • Disposal happens at the end of the containing scope, not right after the next statement.
  • Use the classic using statement when you need a tighter disposal block.