How cached delegates decompile in C#
A method group or a non-capturing lambda does not become a delegate at the call site — the compiler caches one in a hidden static field and reuses it. Here is the exact dup/brtrue/pop/ldftn pattern that caching emits, and how Glass.NET folds it back to a bare name.
Open almost any modern .NET assembly in a decompiler and you will find classes you never wrote: <>c, <>O, fields named <>9__3_0. They are not junk, and they are not obfuscation. They are the compiler’s delegate cache, and if you read the raw IL you will see something surprising — the single source line xs.Where(IsEven) expands into a nine-instruction branch that checks a static field, and only builds a delegate the first time. Folding that back to the two words you actually wrote is one of the most common things a decompiler does, and it is worth understanding exactly what it is undoing.
The real question
There is no “method group” opcode and no “lambda” opcode. A delegate is always built the same way: load a target, ldftn a method, newobj the delegate’s constructor. What makes the emitted code interesting is that the C# compiler does not want to run that sequence on every call — allocating a fresh delegate each time Where(IsEven) executes would be pure waste when the delegate never changes. So it allocates once, stashes the result in a static field, and guards the allocation with a null check. The decompiler’s job is to recognize that guard-and-cache shape and throw all of it away, leaving the name.
A static method group
Start with the simplest case: passing a static method where a delegate is expected.
public sealed class Filter
{
public IEnumerable<int> Evens(IEnumerable<int> xs) => xs.Where(IsEven);
private static bool IsEven(int n) => n % 2 == 0;
}
IsEven is a method, not a delegate. The conversion to Func<int, bool> is where a delegate is born. Because IsEven is static, its target is fixed for the life of the program, so the compiler caches the delegate in a dedicated nested class named <>O:
.method public instance class [System.Linq]...IEnumerable`1<int32> Evens(...) cil managed
{
ldarg.1
ldsfld class [System.Runtime]System.Func`2<int32,bool> Filter/'<>O'::'<0>__IsEven'
dup
brtrue.s IL_001c
pop
ldnull
ldftn bool Filter::IsEven(int32)
newobj instance void class [System.Runtime]System.Func`2<int32,bool>::.ctor(object, native int)
dup
stsfld class [System.Runtime]System.Func`2<int32,bool> Filter/'<>O'::'<0>__IsEven'
IL_001c:
call class ...IEnumerable`1<!!0> [System.Linq]...Enumerable::Where<int32>(...)
ret
}
Read the stack discipline carefully, because it is the whole signature of the pattern. ldsfld loads the cache field. dup copies it. brtrue.s branches when the copy is non-null, consuming it and leaving the original on the stack — the fast path, where the delegate already exists. If it was null, pop discards the null, then ldnull (the target — null, because the method is static) + ldftn IsEven + newobj builds the delegate, dup copies it, and stsfld writes one copy back to the field. Both paths arrive at IL_001c with exactly one Func<int, bool> on the stack, ready for Where. The field name <0>__IsEven encodes a counter and the target method; the class <>O holds nothing but these fields.
A non-capturing lambda
Now the same shape, but the argument is a lambda that captures no local state:
public IEnumerable<int> Evens(IEnumerable<int> xs) => xs.Where(n => n % 2 == 0);
A lambda body has to live somewhere, so the compiler lowers it into a method on a singleton display class <>c. Because the lambda captures nothing, one shared instance — the static field <>9 — can host every non-capturing lambda in the type, and each one gets its own cache field <>9__N_M and body method <Evens>b__N_M:
.method public instance class ...IEnumerable`1<int32> Evens(...) cil managed
{
ldarg.1
ldsfld class [System.Runtime]System.Func`2<int32,bool> Filter/'<>c'::'<>9__1_0'
dup
brtrue.s IL_0020
pop
ldsfld class Filter/'<>c' Filter/'<>c'::'<>9'
ldftn instance bool Filter/'<>c'::'<Evens>b__1_0'(int32)
newobj instance void class [System.Runtime]System.Func`2<int32,bool>::.ctor(object, native int)
dup
stsfld class [System.Runtime]System.Func`2<int32,bool> Filter/'<>c'::'<>9__1_0'
IL_0020:
call class ...IEnumerable`1<!!0> [System.Linq]...Enumerable::Where<int32>(...)
ret
}
It is the identical dance. The only differences are where the method lives and what the delegate’s target is: instead of ldnull + a static ldftn, you get ldsfld <>9 (the singleton) + an instance ldftn to <Evens>b__1_0. The guard, the dup, the stsfld back into <>9__1_0 — all the same. That symmetry is exactly why a decompiler can treat method groups and non-capturing lambdas with one recognizer.
The recovery heuristic
Glass folds a cached-delegate site back to source when it can match the full shape, not just a piece of it. The recognizer walks the instructions around a delegate newobj and confirms four things:
- A static field is loaded and immediately
dup/brtrue-guarded. The branch target is the point just after astsfldto that same field. This is the cache guard; anything else is an ordinary delegate allocation and is left alone. - The field’s declaring type is a compiler cache —
<>Ofor method groups,<>cfor lambdas — and the field name matches the<N>__Nameor<>9__N_Mpattern. Glass treats these as synthetic and never surfaces them as members. - The delegate is built over a fixed target. For
<>O, the target isldnulland theldftnnames a static method — fold to that method group. For<>c, the target isldsfld <>9and theldftnnames an instance method<Caller>b__N_Mon the display class — decompile that method’s body back into a lambda. - The stored-back field is the same one that was loaded. If the load and store disagree, the shape is not a cache and Glass backs off.
When all four hold, the entire ldsfld/dup/brtrue/pop/.../stsfld region disappears and the argument becomes IsEven or n => n % 2 == 0. The <>O and <>c classes are suppressed from the decompiled tree because every reference to them has been rewritten. In the IL view they are still there, one click away, if you want to see the machinery.
Caveats and the honest limits
The caching is not universal, and the gaps are informative. An instance method group over a variable receiver — xs.Where(this._predicate.Test) where _predicate can differ — is not cached, because a static field cannot hold a delegate that closes over a per-instance target. You will see a plain ldarg/ldfld + ldftn + newobj with no guard, and Glass renders this._predicate.Test as the method group with no cache to undo. That absence is itself a signal: an uncached conversion tells you the target was not provably fixed.
A capturing lambda is also not in <>c. The moment a lambda closes over a local, the compiler must allocate a display-class instance per call to hold those locals, so there is nothing to cache and no <>9 singleton — that is the separate world of closure display classes, with its own recovery story. Delegate caching is specifically the optimization for conversions whose target never varies.
One more subtlety: a single cache field can be shared by multiple call sites that use the same method group, so folding one site must not delete a field another site still reads. Glass only removes a synthetic cache member once every reference to it has been rewritten — if some reference it cannot prove is a cache guard survives, the field stays visible rather than producing code that would not recompile.
Takeaway
The <>O and <>c classes and their <>9__ fields are not something you wrote and not something you need to read — they are the compiler making sure a method group or a stateless lambda allocates its delegate exactly once. The guard-and-cache branch they drive is a fixed, recognizable shape: load a static field, reuse it if set, otherwise build the delegate and store it. Glass matches that shape, verifies the target is fixed, and prints the bare IsEven or n => ... you started with. Knowing what the decompiler erased — and when it deliberately leaves an uncached conversion in place — is the difference between reading the code and reading the compiler’s bookkeeping.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.