How the using statement decompiles in C#
A using statement leaves no keyword in metadata — it becomes a try/finally with a Dispose call. The reference-type form carries a null check; the struct form drops it and uses a constrained call to avoid boxing. Here is the IL, both shapes, and how Glass.NET rebuilds the using.
The using statement is the most-used resource-management construct in C#, and it vanishes completely at compile time. There is no using keyword in IL, no attribute, no scope record — the compiler rewrites it into a try/finally with a Dispose call in the handler. Reconstructing it is a pattern-matching job, and the pattern comes in two distinctly different shapes depending on whether the resource is a class or a struct. Getting the struct case wrong means inventing a heap allocation and a null check that were never there. This post walks the lowering both ways and shows what Glass.NET rebuilds.
What the source looks like
A using guarantees that Dispose runs when control leaves the block, however it leaves — normal exit, early return, or exception. Both the statement form and the C# 8 declaration form express the same guarantee:
using System;
static class Reader
{
// statement form
public static void Run()
{
using (var r = Resource.Acquire())
{
r.Work();
}
}
// declaration form — scope runs to the end of the method
public static void RunDecl()
{
using var r = Resource.Acquire();
r.Work();
}
}
Both of these say the same thing to the compiler: acquire a resource, use it, dispose it no matter what. Neither the word using nor the choice between the two forms survives into the assembly.
What the compiler really emits
Here is Run as Glass shows it in the IL view. The using has become a protected region and a handler:
.method public hidebysig static void Run () cil managed
{
.locals init (class Resource V_0)
IL_0000: call class Resource Resource::Acquire()
IL_0005: stloc.0
.try
{
IL_0006: ldloc.0
IL_0007: callvirt instance void Resource::Work()
IL_000c: leave.s IL_0019
}
finally
{
IL_000e: ldloc.0
IL_000f: brfalse.s IL_0018
IL_0011: ldloc.0
IL_0012: callvirt instance void [System.Runtime]System.IDisposable::Dispose()
IL_0018: endfinally
}
IL_0019: ret
}
Read the handler closely, because it is the whole fingerprint. ldloc.0; brfalse.s is the if (r != null) guard — a using tolerates a null resource and skips disposal rather than throwing. If the resource is non-null, callvirt ...IDisposable::Dispose() dispatches through the interface. The leave.s at the end of the .try is how a protected region exits: it jumps past the handler, running the finally on the way out. That is the complete reference-type shape.
The struct case is a different shape
Now make the resource a value type. A struct that implements IDisposable disposes without ever touching the heap:
public struct Lease : IDisposable
{
public void Use() { /* ... */ }
public void Dispose() { /* ... */ }
}
static void Run()
{
using (var lease = new Lease())
{
lease.Use();
}
}
The handler the compiler emits looks nothing like the reference-type one:
.method private hidebysig static void Run () cil managed
{
.locals init (valuetype Lease V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj Lease
.try
{
IL_0008: ldloca.s V_0
IL_000a: call instance void Lease::Use()
IL_000f: leave.s IL_001f
}
finally
{
IL_0011: ldloca.s V_0
IL_0013: constrained. Lease
IL_0019: callvirt instance void [System.Runtime]System.IDisposable::Dispose()
IL_001e: endfinally
}
IL_001f: ret
}
Two differences, both load-bearing. First, there is no brfalse null check — a non-nullable struct can never be null, so the guard would be dead code and the compiler drops it. Second, instead of boxing the struct to an IDisposable reference and calling through it, the compiler loads the address of the local with ldloca.s and prefixes the call with constrained. Lease. The constrained. prefix tells the JIT to dispatch the following callvirt directly on the struct at that address — no box, no allocation, no copy. The exact same two-instruction idiom covers a generic T where T : IDisposable, where the token is constrained. !!T and the JIT picks boxing-free or virtual dispatch at runtime depending on what T turns out to be.
The two shapes, side by side
One keyword, two handler shapes. The decompiler reads the handler and rebuilds the using that produced it — keeping the box-free struct call box-free.
What a naive decompiler shows — and what Glass shows
The failure modes here are not cosmetic. A decompiler that does not understand constrained. might render the struct dispose as an interface call, which implies boxing — turning an allocation-free using into source that, if recompiled naively, would box on every disposal. Worse, a decompiler that applies the reference-type template to a struct resource invents an if (r != null) around a value that can never be null:
// struct resource misread with the reference-type template — WRONG
Lease lease = new Lease();
try { lease.Use(); }
finally { if (lease != null) ((IDisposable)lease).Dispose(); } // boxes; null test is nonsense
Glass reads the constrained. token and the absence of the brfalse, and reconstructs the real intent for both resource kinds:
using (var r = Resource.Acquire()) // reference type
{
r.Work();
}
using (var lease = new Lease()) // struct — no box, no null guard
{
lease.Use();
}
It recognises the handler by its contents: an optional null test, a Dispose call (direct callvirt for a reference type, constrained.-prefixed for a value type or type parameter), and endfinally — nothing else. When several usings stack, the compiler nests one try/finally per resource, and Glass unwinds the nest back into the comma or stacked-declaration form the developer wrote.
Caveats and when it breaks
The honest limit is that a using and a hand-written try/finally that disposes the same resource compile to identical IL. There is no bit that distinguishes them. When the handler is exactly the dispose pattern, Glass renders the compact using, because that is overwhelmingly what the source said and it reads better — but if you genuinely wrote the long try/finally by hand, the decompilation will show using instead. The same ambiguity applies between the statement form and the declaration form when the block happens to run to the end of the scope: the IL is the same, and Glass picks one.
Two more edge cases. A nullable value-type resource, using (Lease? lease = ...), is a third shape again — it carries a HasValue check and disposes through the underlying value, not a reference null test. And a ref struct such as a using over a type with a ref-returning Dispose pattern follows the struct path but can never be boxed at all, so a decompiler that falls back to boxing does not merely mislead — it produces code that will not compile.
Takeaway
The using statement is a compiler rewrite, not a runtime feature, and the rewrite is not one shape but two: a null-checked virtual call for reference types, and a box-free constrained. call for value types. A decompiler that keeps only the first shape quietly fabricates allocations and null tests on the struct path. Open an assembly in Glass.NET, select a using-bearing method, and switch between the C# and IL views — line up the constrained. Lease against the struct using, and the brfalse against the reference-type one, and you can watch the keyword reassemble from the handler it was lowered into. Glass is a free .NET decompiler for inspecting code you have the right to inspect — your own assemblies, your dependencies, and binaries you are licensed to analyse — and preserving the difference between those two handlers is what keeps the decompilation faithful to what the program actually does.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.