# What marks an extension method when decompiled


An extension method is one of the purest examples of C# sugar that evaporates in IL. You write `this` on the first parameter, call it as `text.WordCount()`, and feel like you added a method to `string`. The runtime sees none of that. There is a static method, there is a static call, and there is one attribute — `[Extension]` — left behind like a note that says *this could have been written with a dot*. A decompiler's whole job here is to read that note and decide to print the dot, then get the precedence right so a ten-operator LINQ pipeline comes back as a pipeline and not a ten-deep nest. This post walks it for [Glass.NET](https://delta1labs.com/products/glass).

## The method: a static method wearing one attribute

Start with the definition. A trivial extension on `string`:

```csharp
public static class StringExtensions
{
    public static int WordCount(this string s)
        => s.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).Length;
}
```

Compile it and look at what `this` became. It is gone from the parameter — `s` is an ordinary `string` parameter — and in its place are two `[Extension]` attributes, one on the method and one on the class:

```text
.class public abstract sealed auto ansi beforefieldinit StringExtensions
    extends [System.Runtime]System.Object
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )

    .method public hidebysig static int32 WordCount(string s) cil managed
    {
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
        // ... ldarg.0; call String::Split(...); ldlen; ...
    }
}
```

That is the entire difference between an extension method and a static helper. The method is `public static`, the first parameter is a normal parameter of a normal type, there is no special calling convention. The attribute on the *method* is what lets a decompiler render the first parameter as `this s`; the attribute on the *class* is what tells a consuming compiler the class is worth scanning for extensions in the first place. Strip both and you have `StringExtensions.WordCount(x)` and nothing else; add them and `x.WordCount()` becomes legal too.

## The call site: byte-identical to a static call

Now call it two ways and compare the IL:

```csharp
int a = "the quick brown fox".WordCount();         // fluent
int b = StringExtensions.WordCount("the quick brown fox");   // explicit static
```

Both compile to the same three instructions — load the string, `call` the static method, store:

```text
ldstr      "the quick brown fox"
call       int32 StringExtensions::WordCount(string)
stloc.0
```

There is no second form. The CLR has no concept of an extension call, so the `this` is resolved and discarded at compile time, and the call site keeps no record of which syntax the author used. This is the crux of the whole problem: a decompiler **cannot** learn from the call whether the source was fluent. It can only look at the method being called, see `[Extension]` on it, and *choose* to print the call as a member access on its first argument — `"the quick brown fox".WordCount()` — because that is the idiomatic form and almost always the original. Both renderings are legal and compile identically, so the choice is readability, not correctness. Glass.NET makes it, which is why the output reads like C# a human wrote rather than like a macro expansion.

<figure class="my-8"><svg viewBox="0 0 680 270" role="img" aria-label="Diagram showing an extension method losing its fluent syntax in IL and a decompiler reconstructing it from the Extension attribute. On the left, two source forms — text.WordCount() and StringExtensions.WordCount(text) — both compile down to the same IL: ldstr, call WordCount(string), stloc. The IL keeps no record of which form was used. In the middle, the method metadata carries the Extension attribute on both the static class and the method. On the right, Glass reads the Extension attribute, decides the static call should be printed as a member access on its first argument, and renders text.WordCount(). A lower band shows a LINQ chain: the IL nest call ToList of call Select of call Where of source unrolls, because each operator carries Extension, into source.Where().Select().ToList() — a chain, not a nest. A note states the attribute, not the call site, is the source of truth." style="width:100%;height:auto"><defs><style>.ti{font:600 12px system-ui;fill:#1547e0}.h{font:600 11px system-ui;fill:#0f172a}.x{font:10px system-ui;fill:#475569}.mono{font:10px ui-monospace,Consolas,monospace;fill:#1e293b}.src{fill:#eef4ff;stroke:#8eb4ff}.il{fill:#f1f5f9;stroke:#cbd5e1}.key{fill:#fef3c7;stroke:#fbbf24}.out{fill:#dcfce7;stroke:#4ade80}.ar{stroke:#64748b;stroke-width:1.4;fill:none;marker-end:url(#a)}</style><marker id="a" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 z" fill="#64748b"/></marker></defs><text class="ti" x="12" y="18">Fluent syntax evaporates; the [Extension] attribute brings it back</text><rect class="src" x="12" y="30" width="178" height="30" rx="5"/><text class="mono" x="20" y="49">text.WordCount()</text><rect class="src" x="12" y="66" width="178" height="30" rx="5"/><text class="mono" x="18" y="85">StringExtensions.WordCount(text)</text><rect class="il" x="236" y="36" width="164" height="54" rx="5"/><text class="mono" x="244" y="55">ldstr text</text><text class="mono" x="244" y="70">call WordCount(string)</text><text class="mono" x="244" y="85">stloc.0</text><path class="ar" d="M190,45 C214,45 214,56 236,56"/><path class="ar" d="M190,81 C214,81 214,70 236,70"/><rect class="key" x="236" y="104" width="164" height="42" rx="5"/><text class="h" x="244" y="122">metadata</text><text class="mono" x="244" y="138">[Extension] on class + method</text><rect class="out" x="446" y="52" width="222" height="52" rx="5"/><text class="h" x="456" y="72">Glass output</text><text class="mono" x="456" y="92">text.WordCount()</text><path class="ar" d="M400,62 L446,70"/><path class="ar" d="M400,125 C424,125 424,92 446,92"/><rect class="il" x="12" y="172" width="320" height="78" rx="5"/><text class="h" x="22" y="190">IL: an outermost-last nest</text><text class="mono" x="22" y="210">call ToList(</text><text class="mono" x="34" y="225">call Select(</text><text class="mono" x="46" y="240">call Where(source, p), f))</text><rect class="out" x="356" y="172" width="312" height="78" rx="5"/><text class="h" x="366" y="190">each operator has [Extension] → unroll</text><text class="mono" x="366" y="214">source.Where(p)</text><text class="mono" x="430" y="230">.Select(f)</text><text class="mono" x="480" y="246">.ToList()</text><path class="ar" d="M332,211 L356,211"/></svg></figure>

## The chain: one rule, applied outward

The reason the dot matters is LINQ, where reading the IL literally produces something correct and unreadable. Take a pipeline:

```csharp
var top = people.Where(p => p.Active).Select(p => p.Name).OrderBy(n => n).ToList();
```

Every one of `Where`, `Select`, `OrderBy`, `ToList` is a static extension method on `Enumerable`, so the IL is a nest — the outermost call emitted last, each result feeding the next call's first argument:

```text
call ...ToList( call ...OrderBy( call ...Select( call ...Where(people, <lambda>), <lambda>), <lambda>))
```

Read that literally and you get `Enumerable.ToList(Enumerable.OrderBy(Enumerable.Select(Enumerable.Where(people, …), …), …))` — a four-deep nest you have to read inside-out. Because each method carries `[Extension]`, Glass.NET applies the one rule repeatedly: print each static call as a member access on its *first argument*. The innermost `Where(people, …)` becomes `people.Where(…)`; that whole expression is the first argument of `Select`, so it reads `people.Where(…).Select(…)`; and so on outward until the nest has unrolled into the original chain. The precedence has to be exact — a lambda body with a conditional, or a cast inserted mid-chain, needs the right parentheses or the meaning shifts — but the *shape* falls straight out of applying "member access on the first argument" from the innermost call out.

## Why the attribute, not the call, is the source of truth

It is worth being precise about what Glass trusts. The call site is ambiguous by construction — it is the same IL for both syntaxes — so it carries no signal. The method's metadata is unambiguous: `[Extension]` is present or it is not. So Glass keys its decision on the method:

- Target method has `[Extension]` → render the call as `arg0.Method(rest)`.
- Target method does not → render it as `Type.Method(args)`.

This also handles the awkward real cases correctly. If someone deliberately wrote `Enumerable.Where(list, p)` in explicit static form, Glass still prints `list.Where(p)` — which is fine, because it compiles back to the same IL and reads better. And a *generic* extension method like `Select<TSource, TResult>` carries the attribute just the same; Glass renders `src.Select(f)` and lets C#'s type inference recover the type arguments at recompile, only spelling them out when inference cannot. The attribute is a reliable, local signal on the method being called, which is exactly what a decompiler wants — no global analysis, no guessing from surrounding `using` directives that are not even in the IL.

## The C# 14 extension-block shape

Newer code may use extension blocks, which group members under one receiver and, for the first time, allow extension *properties*:

```csharp
public static class StringExtensions
{
    extension(string s)
    {
        public int  WordCount => s.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries).Length;
        public bool IsBlank()  => string.IsNullOrWhiteSpace(s);
    }
}
```

This is new syntax, not new runtime. The members still lower to static methods on the enclosing static class, marked so a consumer's compiler can route `name.WordCount` and `name.IsBlank()` to them — an extension property becomes a static accessor method, an extension method a static method with the receiver as its first parameter, plus grouping metadata tying several members to one receiver. A decompiler reading such an assembly sees static methods with marker metadata and a receiver parameter, much as before. Glass.NET reconstructs them as extensions; a plain extension method can be shown either as the classic `this`-parameter form or inside a block, but an extension *property* has no classic spelling, so it comes back as a block. The semantics are identical either way, because [the lowered static methods are identical](https://delta1labs.com/blog/decompile-static-abstract-interface-members) — the skin changed, not the IL.

## The takeaway

An extension method leaves exactly one trace in metadata: the `[Extension]` attribute, on the method so the first parameter can wear `this`, and on the class so a compiler knows to look there. The call site keeps no trace at all — `text.WordCount()` and `StringExtensions.WordCount(text)` are byte-identical IL — so a decompiler cannot learn the fluent form from the call and must read it off the method instead. Glass.NET does exactly that: it prints any static call whose target carries `[Extension]` as a member access on the first argument, applies that one rule from the innermost call outward to unroll a LINQ nest back into a chain, and recognises the C# 14 extension-block lowering as the same static methods under a newer syntax. The result reads like the pipeline that was written, not the nest the compiler left — because the decompiler trusted the attribute, which is the only thing that survived.

