Editorial illustration.
Replacing nested using blocks with using var can make C# code easier to read, but the lifetime boundary changes visually: each declaration is disposed at the end of its enclosing scope. If several resources depend on each other, cleanup order matters. Before refactoring, work out where each object is disposed and whether an exception or early return changes that boundary.
Summary
- A
usingdeclaration disposes its resource when execution leaves the enclosing scope. - Multiple declarations in one scope dispose in reverse declaration order.
- A
usingstatement limits disposal to its embedded block, which can make lifetime easier to see. - Both patterns work with
IDisposable;await usingapplies toIAsyncDisposablein supported C# versions.
A small example
This example targets .NET 8 or later, uses top-level statements (C# 9 or later), and uses using declarations (C# 8 or later):
using System.Text;
var path = "report.txt";
using var stream = File.OpenRead(path);
using var reader = new StreamReader(stream, Encoding.UTF8);
var contents = reader.ReadToEnd();
Console.WriteLine(contents.Length);
When the method or top-level program scope ends, reader is disposed first and stream second. That order is useful: the reader may still need the stream while it is being disposed. In this particular example, StreamReader also owns the stream by default, so explicitly declaring both can be redundant or create ownership confusion. A simpler version is usually enough:
using var reader = File.OpenText("report.txt");
var contents = reader.ReadToEnd();
Console.WriteLine(contents.Length);
The example demonstrates syntax, not a universal reason to declare every nested resource separately.
How the lifetime differs from a using statement
A using statement scopes disposal to its body:
using (var reader = File.OpenText("report.txt"))
{
Console.WriteLine(reader.ReadToEnd());
}
// The reader is disposed here, before code continues.
A declaration keeps the variable in the surrounding block:
using var reader = File.OpenText("report.txt");
Console.WriteLine(reader.ReadToEnd());
// Other code in this block can still use reader.
The compiler implements the cleanup with try/finally behavior, so disposal runs when control leaves the scope through normal completion, return, or an exception. The resource is not disposed at the end of the statement where it was declared.
That distinction often matters in a long method. If a file handle should be released before a later operation, use a smaller block or a using statement so the boundary is explicit.
Disposal order and ownership
When declarations share a scope, cleanup runs in reverse order of declaration. This is the same stack-like principle as nested using statements. Declare a dependent object after the resource it uses so the dependent object is disposed first.
Do not dispose the same resource twice unless its contract says that is safe. Many framework types tolerate repeated disposal, but your own types may not. Also decide who owns a resource: if a method receives a stream from its caller, disposing it can break the caller’s work. Some constructors expose a leaveOpen option precisely to make ownership explicit.
A useful review question is: “At what exact brace does this resource stop being usable?” If the answer is difficult to see, introduce a block and make the lifetime shorter.
Async cleanup
For resources implementing IAsyncDisposable, C# supports await using:
await using var connection = await OpenConnectionAsync();
await connection.ExecuteAsync("SELECT 1");
The enclosing method must be asynchronous, and the object must provide asynchronous disposal. The compiler awaits cleanup when the scope exits. Do not add await using just because another operation is asynchronous: check the resource’s actual interface and library guidance.
using declarations are a C# language feature; IDisposable and IAsyncDisposable are .NET interfaces. The exact API surface depends on your target framework. Compile against the project’s configured target framework and language version rather than assuming every example works in an older project.
Refactor safely
- Find where the resource is first created and where it is currently disposed.
- Check whether another object owns it or depends on it during disposal.
- Identify early returns and exceptions that leave the block.
- Replace nested syntax only if the new scope ends at the same or a deliberate later point.
- Build and run the project’s existing tests or a small usage path, especially when the resource controls a file, connection or transaction.
Prefer the compact declaration when the method has one clear lifetime. Prefer an explicit block when a short lifetime reduces lock duration, file contention, connection occupancy or ambiguity.
For more C# examples involving cancellation and asynchronous work, see our CancellationToken guide and bounded channels overview.
My Take
I use using var when it makes a short method easier to scan. In a method that opens several dependent resources or does meaningful work after the first resource is no longer needed, a small explicit block communicates intent better. Fewer braces are not automatically clearer if the cleanup point disappears from view.
Conclusion
A using declaration is disposed at the end of its enclosing scope, and multiple declarations unwind in reverse order. Check ownership and lifetime before refactoring, especially for streams, transactions and connections. Use the narrower scope that matches when the resource is actually needed.