# How deconstruction decompiles back to C#


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](https://delta1labs.com/products/glass), 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:

```csharp
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:

```csharp
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.

<figure class="my-8"><svg viewBox="0 0 760 290" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Decompiler decision: a void Deconstruct call with all-out parameters becomes a deconstruction, while Item1 and Item2 field reads off a ValueTuple become a tuple deconstruction"><rect x="20" y="20" width="220" height="110" rx="8" fill="none" stroke="#6366f1" stroke-width="2"/><text x="130" y="46" text-anchor="middle" fill="#6366f1" font-size="13" font-weight="bold">IL shape seen</text><text x="130" y="72" text-anchor="middle" fill="#64748b" font-size="12">call void</text><text x="130" y="90" text-anchor="middle" fill="#64748b" font-size="12">T::Deconstruct(int32&amp;,</text><text x="130" y="108" text-anchor="middle" fill="#64748b" font-size="12">int32&amp;)</text><rect x="20" y="160" width="220" height="110" rx="8" fill="none" stroke="#0891b2" stroke-width="2"/><text x="130" y="186" text-anchor="middle" fill="#0891b2" font-size="13" font-weight="bold">IL shape seen</text><text x="130" y="212" text-anchor="middle" fill="#64748b" font-size="12">ldfld ...::Item1 → stloc</text><text x="130" y="230" text-anchor="middle" fill="#64748b" font-size="12">ldfld ...::Item2 → stloc</text><text x="130" y="248" text-anchor="middle" fill="#64748b" font-size="12">(no call)</text><path d="M240 75 L360 75" stroke="#6366f1" stroke-width="2" marker-end="url(#a)"/><path d="M240 215 L360 215" stroke="#0891b2" stroke-width="2" marker-end="url(#a)"/><rect x="360" y="40" width="380" height="80" rx="8" fill="#6366f1"/><text x="550" y="68" text-anchor="middle" fill="#ffffff" font-size="13" font-weight="bold">user-defined deconstruction</text><text x="550" y="96" text-anchor="middle" fill="#ffffff" font-size="13" font-family="monospace">var (x, y) = point;</text><rect x="360" y="178" width="380" height="80" rx="8" fill="#0891b2"/><text x="550" y="206" text-anchor="middle" fill="#ffffff" font-size="13" font-weight="bold">tuple deconstruction</text><text x="550" y="234" text-anchor="middle" fill="#ffffff" font-size="13" font-family="monospace">var (a, b) = MinMax(1, 2);</text><defs><marker id="a" markerWidth="9" markerHeight="9" refX="7" refY="4.5" orient="auto"><path d="M0 0 L9 4.5 L0 9 z" fill="#64748b"/></marker></defs></svg><figcaption class="text-sm text-muted mt-2">Two IL shapes, two C# forms. Glass keys off the method signature and usage, not a keyword.</figcaption></figure>

## 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:

```csharp
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:

```csharp
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:

```csharp
(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](https://delta1labs.com/products/glass), and the `(x, y)` comes back wherever the `Deconstruct` call or the `ItemN` reads say it belongs.

