Skip to content
← All posts
· Delta1 Labs Glass.NETDecompilationC#.NET

How deconstruction decompiles back to C#

There is no deconstruction opcode in .NET. var (x, y) = point is lowered to a Deconstruct(out, out) call plus local assignments, and tuple deconstruction is just Item1/Item2 field reads. Here is how Glass.NET, the free decompiler, tells the two patterns apart and rebuilds the syntax — including discards and nested targets.

Modern C# lets you unpack a value in one line: var (x, y) = point;. It reads like a primitive — as if the CLR knew how to split a Point into two integers. It does not. There is no deconstruction opcode, no “unpack” instruction, nothing in the metadata that says this struct deconstructs into two ints. By the time the compiler is finished, the whole thing is an ordinary method call and a couple of local stores. Rebuilding the (x, y) syntax from that is a precise bit of pattern recognition, and it is a clean way to watch how Glass.NET, the free .NET decompiler, turns a C# convention back into C#.

The real question

Two very different C# statements produce almost the same IL shape:

var (x, y) = point;        // user-defined: point has a Deconstruct method
var (a, b) = MinMax(1, 2); // ValueTuple: MinMax returns (int, int)

Both end with two integers sitting in two locals. But one went through a method call and the other went through field reads, and a decompiler has to tell them apart to print the right source. Get it wrong and you either hide a real method call or invent one that was never there.

The user-defined case: a Deconstruct call

Start with a type that opts in. Deconstruction is not magic metadata — it is a method named Deconstruct that returns void and hands every result back through an out parameter:

readonly struct Point
{
    public int X { get; }
    public int Y { get; }
    public Point(int x, int y) { X = x; Y = y; }
    public void Deconstruct(out int x, out int y) { x = X; y = Y; }
}

static int Sum(Point p)
{
    var (x, y) = p;
    return x + y;
}

The var (x, y) = p; line carries no trace of itself into the IL. Here is Sum:

.method private hidebysig static int32 Sum(valuetype Point p) cil managed
{
  .locals init (int32 V_0,   // x
                int32 V_1)   // y
  ldarga.s   p
  ldloca.s   V_0
  ldloca.s   V_1
  call       instance void Point::Deconstruct(int32&, int32&)
  ldloc.0
  ldloc.1
  add
  ret
}

That is the entire deconstruction: push the address of p, push the addresses of two fresh locals, call Deconstruct, and the locals now hold the two halves. The (x, y) tuple syntax has evaporated into a normal call.

To go back, Glass matches a specific signature-and-usage shape rather than a keyword. The method is literally named Deconstruct; it returns void; every parameter is out (int32& with the [out] flag); and each out argument is the address of a local that is assigned by this call and nothing else. When all of that holds, Glass collapses the call plus its target locals into one deconstruction assignment and recovers var (x, y) = p;. The names x and y come from the PDB when one is present; without symbols Glass synthesizes stable names.

IL shape seencall voidT::Deconstruct(int32&,int32&)IL shape seenldfld ...::Item1 → stlocldfld ...::Item2 → stloc(no call)user-defined deconstructionvar (x, y) = point;tuple deconstructionvar (a, b) = MinMax(1, 2);
Two IL shapes, two C# forms. Glass keys off the method signature and usage, not a keyword.

The tuple case: no call at all

Now the other shape. When the right-hand side is a ValueTuple, there is no Deconstruct method to call — the compiler just reads the tuple’s fields:

static (int, int) MinMax(int a, int b) => a < b ? (a, b) : (b, a);

static int Spread(int a, int b)
{
    var (lo, hi) = MinMax(a, b);
    return hi - lo;
}

Spread compiles to:

.method private hidebysig static int32 Spread(int32 a, int32 b) cil managed
{
  .locals init (int32 V_0,   // lo
                int32 V_1,   // hi
                valuetype [System.Runtime]System.ValueTuple`2<int32,int32> V_2)
  ldarg.0
  ldarg.1
  call       valuetype ...ValueTuple`2<int32,int32> Spread::MinMax(int32, int32)
  stloc.2
  ldloc.2
  ldfld      !0 valuetype ...ValueTuple`2<int32,int32>::Item1
  stloc.0
  ldloc.2
  ldfld      !1 valuetype ...ValueTuple`2<int32,int32>::Item2
  stloc.1
  ldloc.1
  ldloc.0
  sub
  ret
}

No Deconstruct appears anywhere. The call is to MinMax; the deconstruction is purely the two ldfld Item1/ldfld Item2 reads into lo and hi. Glass matches this shape — a tuple-typed temporary whose ItemN fields are each read exactly once, in order, into result locals — and prints var (lo, hi) = MinMax(a, b);. The intermediate V_2 tuple is folded away because it exists only to feed the two field reads.

This is why the distinction matters: if Glass treated every pair of integer stores as a deconstruction it would mangle ordinary code, and if it ignored the ItemN pattern it would leave you reading tuple.Item1 and tuple.Item2 instead of named locals.

Discards and mixed targets

Deconstruction has corners, and they show up in the IL as small variations on the same shapes.

A discard still requires an out argument — the method does not know you are ignoring a slot — so the compiler passes a throwaway local that is written and never read:

var (_, y) = point;

Glass sees an out-local that the Deconstruct call assigns and that no later instruction touches, recognizes it as dead, and prints _ instead of fabricating a variable name.

Mixed targets — assigning into variables that already exist, or into fields and properties — force left-to-right evaluation order, so the compiler stages results in temporaries first:

(this.x, existing) = point;

lowers to Deconstruct into two temps, then this.x = temp0; existing = temp1;. Glass keeps the assignment targets as written and still collapses the temps, so the deconstruction assignment comes back rather than three loose statements.

The same Deconstruct match also drives positional patterns. A point is (0, 0) or a switch arm like (var px, 0) is compiled as a Deconstruct call followed by comparisons on the out-locals; Glass recognizes the call-then-test shape and rebuilds the positional pattern instead of showing the raw out-parameter plumbing.

When the pattern breaks

The reconstruction is only as good as the shape. An extension Deconstruct — defined as static void Deconstruct(this KeyValuePair<K,V> kvp, out K key, out V value) — is a static call, not an instance one; Glass handles it, but a hand-rolled method that merely looks like a deconstruction (right name, right out-parameters) will also be rendered as one, because at the IL level that is indistinguishable from the real thing. Heavy obfuscation that renames Deconstruct breaks the match entirely: with the name gone, Glass can only show the plain out-parameter call, since the deconstruction syntax is a source convention that lives in the method name and nowhere else. And interleaved or reordered stores — from aggressive optimization — can push the field-read sequence out of the exact order Glass expects, in which case it safely falls back to literal Item1/Item2 access rather than guess.

Takeaway

Deconstruction is a reminder that a lot of “modern C#” is convention, not runtime: var (x, y) = p; is a method named Deconstruct and two stores, and tuple unpacking is nothing but ItemN field reads. A decompiler earns the clean syntax by matching those two shapes precisely — signature, out-parameters, single-assignment locals, field order — and folding the scaffolding away. Glass.NET does this statically, reading only metadata and IL, on your own assemblies and the dependencies you are auditing. Open a DLL in the Visual-Studio-native UI, the CLI, or over MCP, and the (x, y) comes back wherever the Deconstruct call or the ItemN reads say it belongs.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.