DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Originally published at delta1labs.com

How the new params collections decompile

You open a C# 13 assembly in Glass.NET, land on a call that your source wrote as Sum(1, 2, 3), and a decompiler that has not caught up shows you something else entirely: a local of some type called <>y__InlineArray3<int>, an initobj, three stores through a helper, a ReadOnlySpan<int> materialized out of it, and only then the call. No array. No brackets. Why does a three-argument call look like a stack-allocation ritual?

Because in C# 13 params no longer means "array." It can mean span.

The real question

For twenty years params had exactly one lowering: params T[] plus a heap array built with newarr at every call site. C# 13 added params collections — params may now apply to ReadOnlySpan<T>, Span<T>, or any collection type that a collection expression can target. The headline case is params ReadOnlySpan<T>, because it lets a variadic call avoid the heap array altogether: the compiler stack-allocates the arguments and passes a span over them.

That changes what a decompiler must read. The old reconstruction keyed off newarr and System.ParamArrayAttribute. A span-based params method has neither. If Glass only knows the old pattern, it cannot prove the call was variadic, so it prints the span plumbing verbatim — correct, but unreadable. Recovering Sum(1, 2, 3) means reasoning from a different attribute and a different call-site shape.

The method signature

Start with the callee. Two overloads, old and new:

using System.Runtime.CompilerServices;

static int SumArray(params int[] numbers)          // C# since forever
{
    int total = 0;
    foreach (int n in numbers) total += n;
    return total;
}

static int SumSpan(params ReadOnlySpan<int> numbers) // C# 13
{
    int total = 0;
    foreach (int n in numbers) total += n;
    return total;
}
Enter fullscreen mode Exit fullscreen mode

In metadata, the only thing marking either parameter as params is a custom attribute. The old one is System.ParamArrayAttribute; the new one is System.Runtime.CompilerServices.ParamCollectionAttribute. They are distinct types in distinct namespaces, and that single fact is what the recovery hinges on.

.method private hidebysig static int32 SumArray(int32[] numbers) cil managed
{
  .param [1]
  .custom instance void [System.Runtime]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )
  // ...
}

.method private hidebysig static int32
        SumSpan(valuetype [System.Runtime]System.ReadOnlySpan`1<int32> numbers) cil managed
{
  .param [1]
  .custom instance void
          [System.Runtime]System.Runtime.CompilerServices.ParamCollectionAttribute::.ctor() = ( 01 00 00 00 )
  // ...
}
Enter fullscreen mode Exit fullscreen mode

A decompiler that does not recognize ParamCollectionAttribute sees SumSpan as an ordinary method taking a ReadOnlySpan<int>, with a mystery attribute attached. Everything downstream follows from whether it knows that attribute means params.

The synthesized buffer type

Now the call site. The compiler needs somewhere to put the three arguments. For a span params call it does not allocate int[] — it synthesizes a one-field value type and relies on C# 12's inline-array feature to treat that single field as a fixed-size block. For three elements you get:

.class private sequential ansi sealed beforefieldinit '<>y__InlineArray3`1'<T>
       extends [System.Runtime]System.ValueType
{
  .custom instance void
          [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = ( 01 00 03 00 00 00 00 00 )
  .field private !T _element0
}
Enter fullscreen mode Exit fullscreen mode

Read the attribute blob: InlineArrayAttribute(3). The struct has one field, _element0, but the [InlineArray(3)] marker tells the runtime to lay it out as three contiguous T slots. The element count is baked into both the attribute argument and the type's own name — <>y__InlineArray3 — and nowhere else. The name is compiler-internal and unspeakable in C#; you will never have written it.

The call site

Here is SumSpan(1, 2, 3) as emitted. Watch the arguments go into the buffer one slot at a time, then a span is taken over the whole thing:

.locals init (valuetype '<>y__InlineArray3`1'<int32> V_0)
ldloca.s   V_0
initobj    valuetype '<>y__InlineArray3`1'<int32>
ldloca.s   V_0
ldc.i4.0
call       !!1& '<PrivateImplementationDetails>'::InlineArrayElementRef<valuetype '<>y__InlineArray3`1'<int32>, int32>(!!0&, int32)
ldc.i4.1
stind.i4
ldloca.s   V_0
ldc.i4.1
call       !!1& '<PrivateImplementationDetails>'::InlineArrayElementRef<valuetype '<>y__InlineArray3`1'<int32>, int32>(!!0&, int32)
ldc.i4.2
stind.i4
ldloca.s   V_0
ldc.i4.2
call       !!1& '<PrivateImplementationDetails>'::InlineArrayElementRef<valuetype '<>y__InlineArray3`1'<int32>, int32>(!!0&, int32)
ldc.i4.3
stind.i4
ldloca.s   V_0
ldc.i4.3
call       valuetype [System.Runtime]System.ReadOnlySpan`1<!!1> '<PrivateImplementationDetails>'::InlineArrayAsReadOnlySpan<valuetype '<>y__InlineArray3`1'<int32>, int32>(!!0&, int32)
call       int32 C::SumSpan(valuetype [System.Runtime]System.ReadOnlySpan`1<int32>)
Enter fullscreen mode Exit fullscreen mode

initobj zeroes the stack buffer; each argument is written through InlineArrayElementRef, which returns a managed reference to slot i; InlineArrayAsReadOnlySpan wraps the filled buffer in a ReadOnlySpan<int> of length 3; the final call passes that span. No newobj, no GC allocation — the buffer lives on the stack.

Contrast the old array path for SumArray(1, 2, 3), which does hit the heap:

ldc.i4.3
newarr     [System.Runtime]System.Int32
dup
ldc.i4.0
ldc.i4.1
stelem.i4
dup
ldc.i4.1
ldc.i4.2
stelem.i4
dup
ldc.i4.2
ldc.i4.3
stelem.i4
call       int32 C::SumArray(int32[])
Enter fullscreen mode Exit fullscreen mode

newarr allocates, stelem fills, done. Two completely different shapes that a user wrote identically.

Two lowerings — a stack inline-array buffer and a heap array — reconstruct to the same variadic call. The attribute on the parameter is what lets Glass fold each one back.

How Glass folds it back

Glass treats the span params call as a pattern match gated on the parameter's attribute, not on any opcode. To rewrite a call into variadic form it confirms, in order:

  1. The callee's last parameter carries ParamCollectionAttribute (for the span form) or ParamArrayAttribute (for the classic array form). This is the switch. Without one of these attributes the method is not variadic and the call is printed as written.
  2. The call site builds the argument the expected way. For ParamCollectionAttribute over a ReadOnlySpan<T>, Glass looks for an initobj of a <>y__InlineArrayN local followed by the InlineArrayElementRef stores and an InlineArrayAsReadOnlySpan wrap. The N in the type name, and the [InlineArray(N)] argument, give the element count.
  3. Each slot's stored value is recoverable — here ldc.i4.1/2/3. Glass reads the values written through the per-slot InlineArrayElementRef refs in index order and treats them as the variadic arguments.

When all three hold, Glass drops the buffer local, the helper calls, and the span, and prints SumSpan(1, 2, 3). The synthesized <>y__InlineArray3<int> type is recognized as compiler-internal and suppressed from the decompiled output — it is never shown as a type you could have declared. For the array form the match is simpler: newarr, a run of stelem, and the ParamArrayAttribute on the parameter, folded to the same SumArray(1, 2, 3).

In the IL view you can see the evidence yourself: open the callee, look at .param [1], and read the .custom line. ParamCollectionAttribute tells you the parameter is a C# 13 span params; ParamArrayAttribute tells you it is the classic array params. That one attribute token is the whole signal.

When it breaks

The honest limits are worth stating plainly.

A decompiler that only knows ParamArrayAttribute will never fold the span form. It has no rule that says "an inline-array buffer plus a span wrap is a variadic call," so it faithfully prints the buffer local, the three InlineArrayElementRef stores, and the span. That output is correct — it recompiles — but it exposes the plumbing the language was hiding. This is exactly the "weird struct and a span" that sends people looking for an explanation.

The buffer type names are compiler-internal. <>y__InlineArray3<int> is unspeakable in C# and its name is not contractual; a future compiler could change it. Glass matches on the shape — an [InlineArray(N)] value type with a single field, filled by the recognized helpers — rather than on the literal name, so it survives a rename, but a genuinely novel lowering shape would fall through to the raw rendering.

And the two params forms are not interchangeable at the call site even though they fold to the same text. You can pass an explicit int[] to SumArray where the array is the whole argument list; you cannot hand an arbitrary heap array to a params ReadOnlySpan<int> the same way, because the span form is built for stack buffers and stackalloc-style lifetimes. When Glass shows you SumSpan(1, 2, 3), read it as a span params call, not as sugar for an array you can substitute freely.

Takeaway

C# 13 params collections give you a variadic call with no heap array: the arguments go into a stack-allocated [InlineArray(N)] buffer and the callee receives a ReadOnlySpan<T> over it. The only durable signal that the call was variadic is System.Runtime.CompilerServices.ParamCollectionAttribute on the parameter — a different attribute from the classic System.ParamArrayAttribute, and the thing an older decompiler misses. Glass reads that attribute, recognizes the inline-array call-site shape, and renders M(1, 2, 3) while hiding the compiler-internal buffer. When you see the raw span-over-buffer plumbing in some other tool, you are looking at the mechanism the attribute was meant to let you ignore.

Top comments (0)