# Can you trust a decompiler's nullable annotations


Open an assembly in a decompiler and it shows you `Task<Order?> FindAsync(string id)`. That `?` was never in the bytes it read. To the CLR, `Order?` and `Order` are the same type — one `TypeRef`, one signature blob, not a single bit of difference between them. The question mark is a *reconstruction*. And here is the uncomfortable part: a reconstruction that is wrong still compiles. So the real question is not whether a decompiler can print `?`. It is whether you can trust the ones it prints.

## Where the question mark actually comes from

Nullable reference annotations live entirely outside the type system, in two attributes the compiler embeds privately in every assembly that uses the feature. `NullableAttribute` rides on anything with a type — a field, property, parameter, return, generic parameter, base type — and `NullableContextAttribute` sits on a scope (a type or a method) to set the default for everything inside it. Both speak a three-value byte vocabulary:

| byte | meaning | C# spelling |
|---|---|---|
| `0` | oblivious — no annotation information | compiled without `#nullable enable` |
| `1` | not annotated — non-nullable | `string` |
| `2` | annotated — may be null | `string?` |

The compiler picks the most common byte in a scope as that scope's `NullableContext` and then only emits a `NullableAttribute` where a member *differs* from it. So the attributes you see are the exceptions; the silence is the rule. Reading a `?` back is therefore never a local lookup — it is "resolve the effective context, then apply whatever override this member carries." That indirection is where trust gets hard, because there are about six distinct ways to get it wrong, and all of them produce code that builds.

## The failure modes, shown

**1. The context default, misapplied.** A method's own `NullableContext` beats its type's; a nested type's beats its outer type's; a property accessor's beats the property's. Resolve the chain in the wrong order and a member inherits the wrong default — turning every un-annotated `string` in the method into `string?` or back again. With no context anywhere up the chain the default is `0`, oblivious, which is exactly how every pre-C# 8 assembly must read.

**2. The single-byte versus array constructor.** `NullableAttribute` has two constructors, and a decompiler must handle both:

```
.method public hidebysig specialname rtspecialname instance void .ctor(uint8)   cil managed { }
.method public hidebysig specialname rtspecialname instance void .ctor(uint8[]) cil managed { }
```

When every position of a type shares one byte, the compiler collapses the whole array to the single-byte form. `List<string>` under a context of `1` needs no attribute at all; `List<string?>?` becomes `[Nullable(2)]`, *not* `[Nullable(new byte[]{2,2})]`. Read the single byte as "position zero only" and every nested annotation after the first vanishes.

**3. Value-type slots.** Value types take a position in the flattened walk but always hold `0`, because their nullability is carried by `Nullable<T>` in the signature instead. `Dictionary<int, string?>` flattens to `[1, 0, 2]`, not `[1, 2]`. Skip the slot for `int` and the walk desynchronizes: `string?` reads the `int`'s `0` and comes out non-null.

**4. The array-as-outer-node trap.** The flattening walks the type tree in preorder — the type, then each type argument left to right — and an **array is a node with one child**, its element. Its rank adds nothing; `string?[,]` flattens the same as `string?[]`. Consider a property whose type is `Dictionary<string, string?>[]?`:

```
.custom instance void System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8[]) = ( 01 00 04 00 00 00 02 01 01 02 00 00 )
```

The declared type in the IL is `Dictionary`2<string, string>[]` — not a question mark anywhere, because a signature blob has nowhere to put one. All four annotations are in the attribute: `[2, 1, 1, 2]` — nullable array, non-null `Dictionary`, non-null `string` key, nullable `string?` value. A decompiler that forgets the array is its own node reads only three slots and drops a `?`.

**5. The dropped `notnull` constraint.** A `where T : notnull` constraint has *no runtime representation* — there is no row for it in the `GenericParamConstraint` table. It exists only as a `[Nullable(1)]` on the generic parameter:

```csharp
#nullable enable
public interface IRepository<T> where T : class
{
    T? Find(string id);
    IReadOnlyList<T> All();
    Task<T?> FindAsync(string? id);
}
```

Here the `class` constraint is a flag on the generic parameter, but whether it is `class` or `class?` is *only* the nullable byte: `1` spells `where T : class`, `2` spells `where T : class?`. Drop the byte and you have silently widened the constraint. The same byte drives `T?` on `Find`'s return — an unconstrained-style annotated use of the type parameter.

**6. The oblivious member inside an enabled file.** A member compiled under `#nullable disable` carries context `0`. If the surrounding type is `#nullable enable`, that member must be wrapped in its own `#nullable disable` / `#nullable restore` region, or rendered without a `?` where the author intended none to be stated at all. Flatten everything to `enable` and you assert a contract on code that deliberately made none.

## The only check that proves it: round-trip the bytes

Every failure above shares a property: you cannot catch it by *reading* the decompiled C#, because the C# looks fine. You catch it by rebuilding and comparing the metadata. The contract a library publishes is not the `?`s on screen — it is the flattened byte arrays the compiler will re-emit. So verify those:

<figure class="my-8"><svg viewBox="0 0 720 210" role="img" aria-label="A round-trip verification loop. The original assembly flows into Glass decompile, producing C# source with reconstructed question marks. That source is recompiled into a rebuilt assembly. The nullable bytes are read back out of the rebuilt assembly and diffed against the original assembly's nullable bytes. A matching diff means the contract was preserved; a mismatch means the decompiler changed the contract." style="width:100%;height:auto"><defs><style>.t{font:600 11px system-ui;fill:#6366f1}.s{font:600 11px system-ui;fill:#0f172a}.box{fill:#eef2ff;stroke:#c7d2fe;stroke-width:1.5}.box2{fill:#ecfeff;stroke:#a5f3fc;stroke-width:1.5}.arr{stroke:#64748b;stroke-width:1.5;marker-end:url(#a)}.ok{font:700 11px system-ui;fill:#16a34a}.bad{font:700 11px system-ui;fill:#dc2626}</style><marker id="a" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0,0 L7,3 L0,6 Z" fill="#64748b"/></marker></defs><rect class="box" x="12" y="30" width="118" height="40" rx="6"/><text class="s" x="28" y="54">original .dll</text><rect class="box" x="170" y="30" width="118" height="40" rx="6"/><text class="s" x="188" y="48">Glass</text><text class="s" x="188" y="63">decompile</text><rect class="box" x="328" y="30" width="118" height="40" rx="6"/><text class="s" x="344" y="48">C# with ?</text><text class="s" x="344" y="63">reconstructed</text><rect class="box2" x="486" y="30" width="118" height="40" rx="6"/><text class="s" x="502" y="48">recompile to</text><text class="s" x="502" y="63">rebuilt .dll</text><line class="arr" x1="130" y1="50" x2="168" y2="50"/><line class="arr" x1="288" y1="50" x2="326" y2="50"/><line class="arr" x1="446" y1="50" x2="484" y2="50"/><rect class="box2" x="486" y="120" width="118" height="40" rx="6"/><text class="s" x="498" y="138">read Nullable</text><text class="s" x="498" y="153">bytes back</text><line class="arr" x1="545" y1="70" x2="545" y2="118"/><rect class="box" x="300" y="120" width="146" height="40" rx="6"/><text class="s" x="316" y="138">diff byte arrays</text><text class="s" x="316" y="153">vs original</text><line class="arr" x1="484" y1="140" x2="448" y2="140"/><line class="arr" x1="71" y1="70" x2="330" y2="122"/><text class="ok" x="150" y="180">match → contract preserved</text><text class="bad" x="360" y="192">mismatch → decompiler changed the contract</text></svg><figcaption class="text-sm text-muted mt-2">The round-trip: a faithful reconstruction re-emits byte-identical nullable attributes, so the diff on the public surface — not the look of the C# — is the proof.</figcaption></figure>

In Glass.NET the loop is three concrete steps. Decompile the assembly, then `glass export` the type or the whole assembly to a buildable project. Recompile that project. Then open the original and the rebuilt assembly in Glass's **side-by-side compare**, which diffs them signature by signature — and because the nullable attributes are ordinary custom attributes, a difference in any flattened byte array shows up as a diff on that member. A clean compare on the public surface is a proof, not an impression: the rebuilt library re-emits byte-identical `NullableAttribute` blobs, so a consumer gets exactly the warnings the original author published.

Glass's **C# / IL dual view** is the spot check for a single member. Select `FindAsync`, flip to IL, and the raw `( 01 00 ... )` blob is right there next to the `?`-annotated C# — you can read the bytes yourself and confirm the walk. The embedded `NullableAttribute` and `NullableContextAttribute` definitions themselves are hidden from the C# type tree (they are compiler plumbing, not your code) but remain visible in the IL view, which is where a round-trip check wants them.

## Where verification runs out

Two honest limits. First, `NullablePublicOnlyAttribute`: when the module carries it, the compiler only wrote nullable metadata for the externally visible surface, so you can round-trip the public contract but not the internals — there is no original byte array for a private member to diff against, and a decompiler must render those members oblivious rather than guess. Second, the flow-analysis attributes — `[MaybeNullWhen]`, `[NotNullWhen]`, `[AllowNull]` and the rest — are *not* embedded and do not need reconstruction; they are public BCL types that a decompiler should pass through verbatim. They describe behavior around a value, not the declared nullability of a type, and they are not part of the byte walk at all.

## Takeaway

A `?` on screen is a claim, and a decompiler that reconstructs nullability well is making a checkable one. The check is not whether the code reads plausibly — wrong annotations read perfectly and compile — but whether the rebuilt assembly re-emits the same `NullableAttribute` bytes the original published. Glass.NET is free, static, and built to let you run that check end to end: export, recompile, and diff the bytes, so the question marks you ship are the ones the author actually wrote. See [Glass.NET](https://delta1labs.com/products/glass) and the [decompiler docs](https://delta1labs.com/docs/glass/).

