Skip to content
← All posts
· Delta1 Labs Glass.NETDecompilation.NETDeep Dive

Can you trust a decompiler's nullable annotations

A decompiler that shows you string? reconstructed that question mark — the CLR sees only System.String, and the ? is read back from a byte encoding hidden in NullableAttribute and NullableContextAttribute. There are about six ways to read those bytes wrong, and every one produces C# that compiles while quietly misstating your library's contract. This post walks the failure modes — a misapplied context default, the single-byte versus array constructor, value-type slots, oblivious members, dropped notnull constraints, the array-as-outer-node trap — and shows the only check that actually proves a reconstruction is faithful in Glass.NET: export, recompile, re-read the bytes, diff the contract.

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:

bytemeaningC# spelling
0oblivious — no annotation informationcompiled without #nullable enable
1not annotated — non-nullablestring
2annotated — may be nullstring?

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 Dictionary2<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-nullDictionary, non-null stringkey, nullablestring?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:

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

original .dllGlassdecompileC# with ?reconstructedrecompile torebuilt .dllread Nullablebytes backdiff byte arraysvs originalmatch → contract preservedmismatch → decompiler changed the contract
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.

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 and the decompiler docs.

Try Nebula.NET

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