How compilation works
Follow your code from C# source to IL, assemblies and finally machine code via JIT or AOT.
10 min read
You will learn
- Describe what IL is and why .NET uses it
- Explain what an assembly contains
- Understand JIT and tiered compilation
- Know when ahead-of-time (AOT) compilation helps
Two compilers, not one
In the first lesson you saw the big picture: C# becomes IL, and IL becomes machine code. Let's look closer, because this explains a lot about how .NET behaves — why the first request to a web app is slower, why a .dll runs on any OS, and what "AOT" means.
Program.cs ──Roslyn──▶ MyApp.dll ──JIT (at run time)──▶ machine code
IL + metadata x64 / ARM64Step 1: Roslyn compiles C# to IL
Roslyn — the C# compiler — checks your code (types, syntax, nullability) and emits IL, Intermediate Language: instructions for an imaginary stack-based machine. For example:
int Add(int a, int b) => a + b;compiles to IL like:
ldarg.0 // push argument a onto the stack
ldarg.1 // push argument b
add // pop both, push a + b
ret // return the top of the stackIL is the same on every OS and CPU. That's how one .dll runs on Windows, Linux and macOS.
Fun fact
The editors in this course compile your code with the real Roslyn compiler — running inside your browser.
Step 2: the assembly
The output is an assembly — a .dll (or an .exe launcher plus .dll). It contains:
- IL code for every method,
- Metadata — a complete description of every type, method, property and field,
- A manifest — the assembly's name, version and the other assemblies it depends on.
Metadata is why you can reference a library without its source, why IntelliSense knows its methods, and why reflection works at run time:
C# runtime not loaded
Step 3: the JIT turns IL into machine code
When your program runs, the CLR loads the assembly. The first time a method is called, the JIT compiler translates its IL into native machine code for this CPU, caches it, and jumps to it. Later calls go straight to the cached machine code.
Tiered compilation
Compiling well takes time, so the JIT works in tiers:
- Tier 0 — compile fast with few optimizations, so the app starts quickly.
- Methods that run often (hot loops, busy endpoints) are recompiled at Tier 1 with full optimizations, using information gathered while running.
This is why .NET apps "warm up": they get faster after running for a little while.
Ahead-of-time (AOT) compilation
You can skip the JIT entirely with Native AOT:
dotnet publish -c Release -r linux-x64 -p:PublishAot=trueThe output is a single native executable with no IL and no JIT.
| JIT (default) | Native AOT | |
|---|---|---|
| Startup | Slower (compile on first call) | Very fast |
| Peak performance | Excellent (optimizes using runtime data) | Very good |
| Output | Portable .dll, needs the .NET runtime | One OS-specific executable |
| Dynamic features (loading plugins, some reflection) | Fully supported | Limited |
AOT shines for command-line tools, serverless functions and small containers. For most web apps, the default JIT is the right choice.
Managed code
Because the CLR runs your code, it can do things for you that C or C++ programs must do themselves:
- Memory management — the garbage collector frees objects you no longer use (Track 5).
- Type safety — you can't treat a
stringas anintor read past the end of an array. - Exceptions — errors are reported consistently with stack traces.
Code that runs under the CLR like this is called managed code.
Key takeaways
- Roslyn compiles C# into IL + metadata, stored in an assembly.
- The JIT compiles each method to machine code on first call; tiered compilation optimizes hot code later.
- Native AOT compiles everything ahead of time: fast startup, OS-specific output, fewer dynamic features.
- The CLR manages memory, type safety and exceptions — that's "managed code".
Quiz
4 questions · 3 correct to pass1.What does the C# compiler (Roslyn) produce?
2.When does the JIT compiler translate a method into machine code?
3.Besides IL, what does an assembly contain?
4.What's the main benefit of Native AOT?
To complete this lesson
- ○ Pass the quiz (3/4 correct)