How local functions decompile in C#
A non-capturing local function lowers to a private static method named <Caller>g__Name|0_0; a capturing one spills its captured locals into a <>c__DisplayClassN_0 struct passed by ref — no heap allocation, unlike the class closure a lambda forces. This post reads both shapes field by field, shows the g__ mangled names and the display-class fields, and shows how Glass re-nests the method instead of printing a stray private helper.
Add a small helper function inside a method, compile it, and open the assembly. The helper is not inside anything — it sits on the containing type as a private method with a name you could never type: <Parse>g__Validate|0_0. If it touched any of the outer method’s locals, there is also a type called <>c__DisplayClass0_0 next to it, and your captured variables have become its fields. The lexical nesting you wrote is gone from the metadata entirely. Putting it back is a decompiler’s job, and local functions are one of the cleaner examples of how C# sugar maps to IL.
The real question
The CLR has no notion of a function declared inside another function. A method lives on a type; that is the only place executable code can go. So when you write a local function, the compiler has to answer two questions. Where does the body go? And what happens to the outer locals the body reads?
The answers are a lifted sibling method with a mangled name, and — only if the function captures — a display class that holds the shared locals as fields. Read those two artifacts and the original nesting falls out. The interesting part is that C# has a second tool for the same job, the lambda, and it compiles to something quite different. The difference is why local functions exist, and it is what a decompiler reads to tell the two apart.
The non-capturing case
Start with a local function that captures nothing — it takes everything it needs as parameters:
public static int Parse(string text)
{
int Validate(int n)
{
if (n < 0) throw new FormatException();
return n;
}
return Validate(int.Parse(text));
}
After compilation, Validate is lifted out to a sibling method on the same type. Its name is the tell:
[CompilerGenerated]
private static int <Parse>g__Validate|0_0(int n)
{
if (n < 0) throw new FormatException();
return n;
}
Decode the name piece by piece. <Parse> is the containing method — the angle brackets make it illegal C#, so nothing you write can collide with it and nothing outside the assembly can call it by name. g__ is the compiler’s marker for a local function (the g names its internal “local functions” group, distinct from the b__ it uses for lambda bodies and the c display types). Validate is your identifier, kept verbatim so the name is human-legible. And |0_0 is an ordinal pair: the first number identifies the lexical scope inside Parse, the second disambiguates functions within that scope. A second local function in the same method would be |0_1.
Crucially, the method is static. It captured nothing, so it needs no this and no enclosing state. The call site in Parse is a plain call — the cheapest thing in IL:
ldarg.0
call int32 [System.Runtime]System.Int32::Parse(string)
call int32 Demo::'<Parse>g__Validate|0_0'(int32)
ret
For a decompiler this is almost the whole story. A [CompilerGenerated] method whose name matches <Owner>g__Name|N_M, called only from Owner (and never stored in a delegate or handed out), is a local function. Glass recognises the pattern, strips the mangling back to Validate, and re-nests it inside Parse rather than printing a stray private helper that was never in your source.
The capturing case
Now let the local function read a local of the enclosing method:
public static int Sum(int[] values)
{
int running = 0;
void Add(int v) => running += v;
foreach (int v in values) Add(v);
return running;
}
running is written by Add and read by Sum. It cannot stay a stack local, because Add and Sum must see the same variable. So the compiler creates a display class and moves running into it as a field:
[CompilerGenerated]
private struct <>c__DisplayClass0_0
{
public int running;
}
Note the struct. This is the point of local functions. Because Add is a method the compiler can call directly, it does not need the captured state on the heap — it can pass the display class by ref. The lifted function becomes a static method taking a ref to the struct:
[CompilerGenerated]
private static void <Sum>g__Add|0_0(int v, ref <>c__DisplayClass0_0 c)
{
c.running += v;
}
And Sum itself is rewritten to allocate the struct on its own stack frame, initialise the field, and pass it by ref on every call:
public static int Sum(int[] values)
{
<>c__DisplayClass0_0 c = default;
c.running = 0;
foreach (int v in values)
<Sum>g__Add|0_0(v, ref c);
return c.running;
}
Every reference to running — inside the function and out — now goes through c.running. No object was allocated on the heap. In IL the display class lives in a local slot and is passed with ldloca:
ldloca.s c
ldc.i4.0
stfld int32 Demo/'<>c__DisplayClass0_0'::running
// ... foreach ...
ldloc.1
ldloca.s c
call void Demo::'<Sum>g__Add|0_0'(int32, valuetype Demo/'<>c__DisplayClass0_0'&)
static sibling method; capture adds a <>c__DisplayClass0_0 struct passed by ref with no heap allocation. A lambda instead forces a class display plus a Func<> delegate. Glass keys on the g__ name and the call-only-from-parent fact to re-nest the method.Why this differs from a lambda
Write the same capture as a lambda and the IL changes shape completely:
public static int Sum(int[] values)
{
int running = 0;
Func<int, int> add = v => running += v;
foreach (int v in values) add(v);
return running;
}
Now the compiler cannot use a struct. A Func<int, int> is a delegate object, and a delegate carries a target reference that lives on the heap and can outlive the method. So the display class becomes a class:
[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
public int running;
internal int <Sum>b__0(int v) => running += v;
}
The lambda body is <Sum>b__0 — note b__, not g__ — an instance method on a heap-allocated display class, and add is a Func<> delegate pointing at it. Every call is an indirect callvirt through the delegate, not a direct call. That is the cost local functions were designed to avoid: no delegate object, no guaranteed heap allocation, no indirection. The g__ versus b__ naming and the struct-by-ref versus class-plus-delegate shape are precisely the signals Glass uses to render one as a local function and the other as a lambda — they are genuinely different constructs, not two spellings of one.
How Glass maps it back
The reconstruction is a fixed sequence:
- Match the name. A
[CompilerGenerated]method whose name fits<Owner>g__Name|N_Mis a local function body. Theb__variant marks a lambda; the two never mix. - Confirm the call graph. A local function is only ever called from its owner (and sibling local functions in the same scope), never stored in a delegate or exposed. That containment is what lets Glass re-nest it rather than leave it as a private method.
- Resolve the display class. If the body takes a
ref <>c__DisplayClassN_0(or reads one), Glass maps each field back to the captured local. When the field names survive —running,count— the original variable names come back exactly. Without them, it falls back to positional names. - Re-nest and clean up. The mangling is stripped to
Validate/Add, the function is placed back inside its owner, therefdisplay-class parameter and thec.field prefixes are dissolved back into direct local access, and the result reads like the source you wrote.
From the CLI it is one command:
$ glass export ./App.dll --member "M:Demo.Sum(System.Int32[])"
You get Sum with its nested Add restored. Add --il and Glass shows the honest other side: the lifted <Sum>g__Add|0_0, the <>c__DisplayClass0_0 struct, and the ldloca / ref call sequence.
Caveats and where it gets hard
The name recovery is reliable; the variable recovery is best-effort. The g__Name|N_M pattern is stable across current Roslyn, so re-nesting rarely fails. But the display-class field names are the only record of your original locals, and heavy optimization — or metadata stripped to shrink an assembly — can leave fields with positional names, so a captured count comes back as item1. Loading a PDB or SourceLink restores them; without one, the structure is faithful but the identifiers are not guaranteed.
Three more honest limits. A static local function can never capture, so it never gets a display class — it is always the simple sibling-method case, which is both the easiest to read and the cheapest at runtime. A [MethodImpl(MethodImplOptions.AggressiveInlining)] local function may be inlined into its caller, in which case there is no separate method to re-nest and the body simply appears at the call site. And when several local functions in one method capture overlapping sets of locals, the compiler can share a single display class across them or nest display classes (<>c__DisplayClass0_1 holding a <>c__DisplayClass0_0); untangling which function owns which field is the subtlest part of the job, and the recovered code can be faithful to the IL without being a character-for-character match.
Takeaway
A local function is not nested in the metadata at all — it is a sibling method named <Caller>g__Name|0_0, static when it captures nothing and static taking a ref display-class struct when it does. That struct, <>c__DisplayClass0_0, is how captured locals cross the boundary without a heap allocation, which is the whole reason to reach for a local function over a lambda — the lambda forces a class display and a Func<> delegate instead. Glass.NET reads the g__ name, confirms the function is only called from its parent, maps the display-class fields back to your locals, and re-nests the function as pure static analysis — never running a line. Grab the free build from the Glass.NET download page and open the IL view on the next helper you need to understand, or read the decompilation docs for how the recognisers are wired.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.