Skip to content
← All posts
· Delta1 Labs DecompilationGlass

How Glass tells nint from IntPtr

In metadata, nint and nuint are just System.IntPtr and System.UIntPtr — the same ELEMENT_TYPE_I the runtime has always used. The only thing that records the source spelling is a NativeIntegerAttribute flag array. Here is the encoding, the native-int IL opcodes, and how Glass.NET rebuilds the keyword.

nint and nuint look like fundamental numeric types in C# — you add them, multiply them, cast to and from int, and they size themselves to the platform pointer width. But they are not types. In metadata there is no nint: a field declared nint is System.IntPtr, encoded as the same primitive the CLR has used for native-sized integers since v1. The keyword is a source-level view over an old struct. The only artifact that records you wrote nint instead of IntPtr is a single attribute carrying a boolean flag array — and if a decompiler ignores it, your pointer-width arithmetic comes back spelled as IntPtr, which reads like interop plumbing rather than the numeric code it is. This post walks the encoding and shows what Glass.NET rebuilds.

The source looks like a primitive

Native integers behave like any other integral type. You can declare them, do arithmetic, and convert — no IntPtr.Add, no .ToInt64():

public struct Cursor
{
    public nint Offset;
    public nuint Remaining;

    public nint Advance(nint delta)
    {
        Offset += delta * 2;
        return Offset;
    }

    public static nint FromManaged(int value) => value;

    public static int ToManaged(nint value) => checked((int)value);
}

Two fields — one signed, one unsigned — and three operations: a compound add with a multiply, a widening from int, and a checked narrowing back to int. Nothing here names IntPtr. Yet that is exactly what the compiler writes to metadata.

What the compiler really emits

Here is Cursor as Glass shows it in the IL view. The field and signature types are the giveaway:

.field public native int Offset
.custom instance void [System.Runtime]
    System.Runtime.CompilerServices.NativeIntegerAttribute::.ctor() = ( 01 00 00 00 )

.field public native uint Remaining
.custom instance void [System.Runtime]
    System.Runtime.CompilerServices.NativeIntegerAttribute::.ctor() = ( 01 00 00 00 )

.method public hidebysig instance native int Advance (native int delta) cil managed
{
    IL_0000: ldarg.0
    IL_0001: ldarg.0
    IL_0002: ldfld     native int Cursor::Offset
    IL_0007: ldarg.1
    IL_0008: ldc.i4.2
    IL_0009: conv.i
    IL_000a: mul
    IL_000b: add
    IL_000c: stfld     native int Cursor::Offset
    IL_0011: ldarg.0
    IL_0012: ldfld     native int Cursor::Offset
    IL_0017: ret
}

native int is how ildasm renders ELEMENT_TYPE_I, the primitive signature code for System.IntPtr; native uint is ELEMENT_TYPE_U for System.UIntPtr. So the field type carries no trace of the word nint. The arithmetic is plain integer IL — mul, add — operating on the native-int stack type. The literal 2 is loaded as int32 (ldc.i4.2) and then widened to native width with conv.i before the multiply, because mixing an int and a native int promotes to native. That conv.i is the signed native-int conversion; its unsigned sibling is conv.u.

The two conversions in the static methods complete the picture:

.method public hidebysig static native int FromManaged (int32 value) cil managed
{
    IL_0000: ldarg.0
    IL_0001: conv.i
    IL_0002: ret
}

.method public hidebysig static int32 ToManaged (native int value) cil managed
{
    IL_0000: ldarg.0
    IL_0001: conv.ovf.i4
    IL_0002: ret
}

FromManaged widens int to native int with conv.i. ToManaged narrows with conv.ovf.i4 — the range-checked form, because the source wrote checked((int)value). Drop the .ovf and you have silent truncation instead of an OverflowException; that is the same semantics-carrying suffix covered in the checked-arithmetic walkthrough. None of these opcodes is specific to native int in any way that would distinguish nint arithmetic from IntPtr arithmetic — on modern .NET they are the same operations on the same type.

The flag array that remembers the keyword

So where does nint come from? One attribute: System.Runtime.CompilerServices.NativeIntegerAttribute. The compiler attaches it to every field, parameter, and return whose type contains a native-int position that was written with a keyword. For a single, top-level native int it uses the parameterless constructor — the ( 01 00 00 00 ) blob above is just the attribute prolog with no arguments. That marks “this one IntPtr/UIntPtr was spelled nint/nuint.”

For a composite type — a tuple, an array, a generic — one flag is not enough, because some positions may be nint and others genuine IntPtr. The compiler then emits [NativeInteger(bool[])] with one boolean per IntPtr/UIntPtr occurrence, walked in the same pre-order NullableAttribute and DynamicAttribute use. Consider:

public static (nint offset, IntPtr handle) Split(nint x) => (x, x);

The return type is ValueTuple<IntPtr, IntPtr> — two native-int positions. The compiler decorates it with a flag array { true, false }: the first element was written nint, the second IntPtr. The names come from a separate TupleElementNamesAttribute, exactly as in the tuple-element-names post. Glass combines both to render (nint offset, IntPtr handle) rather than the metadata-literal (IntPtr, IntPtr).

How nint erases to System.IntPtr in metadata and how Glass.NET reads NativeIntegerAttribute to rebuild the keyword 1 · Source spelling nint Offset nuint Remaining (nint, IntPtr) 2 · In metadata native int (I) native uint (U) keyword is gone 3 · Side-channel [NativeInteger] flags { true, false } Glass.NET reconstructs nint Offset; nuint Remaining; flags map each I/U position back to nint or IntPtr

The type that round-trips to the runtime is System.IntPtr; the keyword rides alongside in an attribute, and the decompiler has to read both streams to reproduce the source.

How Glass applies it

Glass resolves the spelling position by position. For each native-int slot in a signature it looks for a NativeIntegerAttribute: a parameterless one, or the next unconsumed flag from the array if the type is composite. A true flag (or the parameterless form) renders nint for ELEMENT_TYPE_I and nuint for ELEMENT_TYPE_U; a false flag — or no attribute at all — renders IntPtr and UIntPtr. Sign is never taken from the attribute; it comes from whether the underlying primitive is I or U. The result reads back as the numeric code it was:

public struct Cursor
{
    public nint Offset;
    public nuint Remaining;

    public nint Advance(nint delta)
    {
        Offset += delta * 2;
        return Offset;
    }

    public static nint FromManaged(int value) => value;

    public static int ToManaged(nint value) => checked((int)value);

    public static (nint offset, IntPtr handle) Split(nint x) => (x, x);
}

Split is the case worth lingering on: Glass keeps IntPtr for the second element precisely because the flag array said so. A decompiler that rendered all of it as nint, or all of it as IntPtr, would be discarding information the compiler took the trouble to record.

When it breaks

The honest limits all stem from the attribute being the only record of intent:

  • No attribute, no keyword. Reference assemblies produced without NativeIntegerAttribute, assemblies from toolchains that predate native ints, and binaries whose attributes an obfuscator has stripped all decompile to IntPtr/UIntPtr. The code is correct and compiles; it just reads as interop rather than arithmetic. Glass will not guess nint where nothing says so.
  • It is cosmetic on modern .NET. Because nint and IntPtr are the same type since .NET 7, choosing one spelling over the other changes nothing about what the code does. This is the inverse of a dropped conv.ovf suffix: losing the keyword costs readability, never behaviour.
  • The flag array must stay aligned. Reconstructing a composite type means consuming flags in the exact pre-order the compiler wrote them. The same discipline applies to NullableAttribute and DynamicAttribute; a decompiler that miscounts a nested generic or an array position will attach the wrong spelling to the wrong slot.

Reading it yourself

Open an assembly in Glass, pick a method that takes or returns a native-sized integer, and switch between the C# and IL views. In IL you will see native int and native uint in the signatures with no sign of nint; in C# you will see the keyword restored. Expand the field or parameter’s custom attributes and find the NativeIntegerAttribute — parameterless for a lone native int, or carrying a bool[] when the type is a tuple, array, or generic. Then strip the attribute in a scratch build and watch Glass fall back to IntPtr: the clearest way to see that the keyword is a side-channel, not a type.

Glass.NET is a free .NET decompiler for code you have the right to inspect — your own assemblies, your dependencies, and third-party binaries you are licensed to analyse. Reading the attribute streams alongside the primitive signatures is what turns IntPtr plumbing back into the native-integer arithmetic the author actually wrote.

Try Nebula.NET

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