Reading top-level statements in decompiled C#
A file of top-level statements has no visible class and no visible Main, but the assembly does. The C# compiler wraps your statements in an internal class named Program and an entry-point method with the unspeakable name <Main>$, marked with the .entrypoint directive. Here is exactly what the compiler emits for the sync and async cases, why the entry point is the method you can never write by hand, where your global usings went, and how Glass.NET reads it back as the top-level program you wrote.
A C# file that uses top-level statements looks like it has no class and no Main — you just write statements and the program runs. But an assembly has to have an entry point, and the metadata has to name a type to hang it on. The compiler supplies both for you, and when you open the result in a decompiler the scaffolding becomes visible again: a class named Program you never wrote, and an entry-point method with a name you cannot write.
This is a working deep-dive on that scaffolding — what the C# compiler emits for a top-level program in both the synchronous and async cases, the .entrypoint tell that identifies the real entry point regardless of its name, where your using directives disappear to, and how Glass.NET reads it all back into the top-level program you started with. Glass is a free .NET decompiler, and everything here is pure static analysis: reading metadata and IL from an assembly you have the right to inspect, never running it.
The source
As plain as a top-level program gets — one statement and an exit code:
using System;
Console.WriteLine($"args: {args.Length}");
return 0;
No namespace, no class, no method signature, and yet args is in scope and return sets the process exit code. All three are things the compiler arranges on your behalf.
What the compiler emits
The statements become the body of a method the compiler names <Main>$, placed on a synthesized class named Program. Decompiled back to readable C#, the generated shape is this:
internal class Program
{
private static int <Main>$(string[] args)
{
Console.WriteLine($"args: {args.Length}");
return 0;
}
}
Two details are worth stopping on. The class is internal (not static), so it carries a default instance constructor even though the entry point itself is static — a hand-written Program with a static Main is usually a static class or has a constructor you chose, so the plain internal class with a compiler-named method is a tell in itself. And the method name, <Main>$, is not a legal C# identifier: the angle brackets and the dollar sign make it unspeakable, so it can never collide with a Main you might add and you can never invoke it by name.
Here is the IL, where the decisive marker lives:
.class private auto ansi beforefieldinit Program
extends [System.Runtime]System.Object
{
.method private hidebysig static int32
'<Main>$'(string[] args) cil managed
{
.entrypoint
.maxstack 8
ldstr "args: "
ldarg.0
ldlen
conv.i4
box [System.Runtime]System.Int32
call string [System.Runtime]System.String::Concat(object, object)
call void [System.Console]System.Console::WriteLine(string)
ldc.i4.0
ret
}
}
The .entrypoint directive is the one that matters. Exactly one method in an executable carries it, and it is what the CLR reads to decide where execution begins. A decompiler does not need to recognize the <Main>$ name convention to find the entry point — it reads the metadata’s entry-point token straight from the CLI header and lands on this method. The name is a convenience for humans; .entrypoint is the ground truth.
When the program is async
Add an await anywhere in the top-level statements and the entry point changes shape. Suppose the source is:
using System;
using System.Threading.Tasks;
await Task.Delay(100);
Console.WriteLine("done");
return args.Length;
Because the body awaits and returns an int, the compiler emits <Main>$ as an async Task<int> method and lowers it exactly like any async method: a generated state-machine struct holds the logic, and <Main>$ itself becomes a thin kickoff. Decompiled, the visible method is just:
[AsyncStateMachine(typeof(<<Main>$>d__0))]
[DebuggerStepThrough]
private static Task<int> <Main>$(string[] args)
{
<<Main>$>d__0 stateMachine = default;
stateMachine.args = args;
stateMachine.<>t__builder =
AsyncTaskMethodBuilder<int>.Create();
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
The real control flow — the await, the continuation after Task.Delay, the return — lives in <<Main>$>d__0.MoveNext(), the same [CompilerGenerated] state-machine pattern used for every async method. The only thing special about it is the mangled base name <Main>$ threaded through the state machine’s own name, <<Main>$>d__0. The runtime still starts at the synchronous <Main>$ wrapper, which the C# language rules quietly adapt to an async entry point for you.
Where the using directives went
One thing you will not find in the metadata is your using directives — least of all the global ones. Implicit usings (<ImplicitUsings>enable</ImplicitUsings>) and explicit global using directives are a compile-time binding convenience; they resolve type names during compilation and emit no metadata and no IL. By the time the assembly exists, every type reference is already fully qualified in the metadata tables.
So the using System; block you see at the top of Glass’s output is not a copy of the original source — it is Glass’s own reconstruction, computed from the fully qualified references the method actually uses. That is the correct mental model for all decompiled using lists: they are regenerated to make the output compile and read cleanly, not recovered from the source. Reading them as evidence of what the author wrote is a mistake.
How Glass reads it back
A naive decompiler stops at the literal truth: an internal class Program with a method named <Main>$. That is accurate but awkward — the angle-bracketed name does not round-trip to compilable C#, and the scaffolding obscures the three lines you care about.
Glass.NET recognizes the pattern — the .entrypoint method, the <Main>$ name convention, and the synthesized Program container — and offers you the program the way it was written: as top-level statements, with the return mapped to the exit code and args presented as the in-scope parameter. When the entry point is async, Glass folds the kickoff and the <<Main>$>d__0 state machine back into the awaiting statements, so you read await Task.Delay(100) instead of a builder dance. You can still drop to the raw IL and see .entrypoint for yourself whenever you want to confirm what the runtime actually starts.
The practical takeaway
When you open an executable and the entry point looks strange — an internal class Program you would not have written, a method named <Main>$, or a short async wrapper in front of a <<Main>$>d__0 struct — you are looking at ordinary top-level statements, not anything exotic. Find the method marked .entrypoint, read its body (or let Glass fold it back to top-level form), and ignore the reconstructed using block as scaffolding rather than source. Glass.NET is free and does this purely by reading the assembly’s metadata and IL, so you can inspect what a program you have the right to analyze actually does at startup — without ever running it.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.