Phase 1 of 30 · Topic 1.1

C# Compilation Pipeline: Roslyn Compiler & IL Disassembly

1Concept

The Roslyn compiler translates C# source code into Common Intermediate Language (CIL/IL) stored in PE assemblies (.dll/.exe). The Common Language Runtime (CLR) validates type safety and uses RyuJIT to emit native x86-64 machine instructions.

2Architecture Diagram

[ Source Code: .cs ]
          │  Roslyn Compiler (csc)
          ▼
   [ PE Assembly (.dll) ] ── Contains CIL Bytecode + Metadata Tables
          │  Loaded into CLR
          ▼
   [ RyuJIT Compiler ] ── Compiles IL on first invocation
          │
          ▼
   [ Native Machine Code ] ── Direct Execution on CPU

3Code Example

C# 13 & .NET 9
using System;

public class JitInspection
{
    public static void Main()
    {
        Console.WriteLine("CLR Execution Pipeline Active.");
        int result = FastAdd(15, 27);
        Console.WriteLine($"Computed: {result}");
    }

    // Roslyn generates: ldarg.0, ldarg.1, add, ret
    public static int FastAdd(int a, int b) => a + b;
}

4Expected Output

CLR Execution Pipeline Active.
Computed: 42

5Key Takeaways

  • Roslyn produces platform-agnostic IL bytecode; CLR produces machine code at runtime.
  • Use `ildasm` or `dotnet-ildasm` to inspect IL opcodes.
  • Metadata tables in assemblies describe all types, methods, fields, and security attributes.