DEV Community

DIVAKAR UPADHYAY
DIVAKAR UPADHYAY

Posted on Originally published at devladder.pages.dev

Compilation: Roslyn, IL, JIT, tiered compilation, ReadyToRun and Native AOT (.NET Internals)

👉 Full lesson with practice questions on DevLadder (free, lesson 3 of 11 in the .NET Internals: CLR, CTS, CLS, IL, JIT, Memory Management & Garbage Collection course)

public static int Add(int a, int b) => a + b;
Enter fullscreen mode Exit fullscreen mode

compiles to IL (stack-based, CPU-independent):

.method public hidebysig static int32 Add(int32 a, int32 b) cil managed
{
  ldarg.0      // push a
  ldarg.1      // push b
  add          // pop two, push sum
  ret          // return top of stack
}
Enter fullscreen mode Exit fullscreen mode

and at runtime the JIT turns it into native code for the actual CPU:

; x64
lea eax, [rcx+rdx]
ret
Enter fullscreen mode Exit fullscreen mode
Technique When code becomes native Pros Cons
JIT (default) At first call of each method Optimises for the actual CPU and runtime profile Startup cost ("warm-up")
Tiered compilation (.NET Core 3+) Tier 0 quick JIT first, tier 1 optimised for hot methods (after ~30 calls), with Dynamic PGO (on by default since .NET 8) Fast startup and fast steady state More complex
ReadyToRun (R2R) Precompiled native code inside the DLL; JIT can still re-optimise Faster startup, still flexible Larger files
Native AOT (.NET 7+) Whole app compiled to a native executable at publish Instant start, small memory, no JIT, no runtime install No dynamic code loading, reflection limits (trimming), longer builds
NGen (.NET Framework) Native images in a machine cache Faster startup Fragile, Framework-only
dotnet publish -c Release -r linux-x64 -p:PublishReadyToRun=true
dotnet publish -c Release -r win-x64 -p:PublishAot=true
Enter fullscreen mode Exit fullscreen mode

Interview questions on .NET Internals

What is the CLR and what does it do?

The Common Language Runtime is .NET's execution engine. It loads assemblies, verifies type safety, JIT-compiles IL to native code, manages memory with the garbage collector, handles exceptions, threads and synchronisation, provides interop with native code and exposes debugging/profiling services. Code running under it is "managed code".

What are CTS and CLS?

CTS (Common Type System) defines how types are declared and used in .NET: value vs reference types, inheritance, members, visibility, so all languages share one type system (C# int = VB.NET Integer = System.Int32). CLS (Common Language Specification) is a subset of CTS rules that every .NET language supports, so a CLS-compliant library can be used from any language (e.g. avoid public unsigned types or names differing only in case).

What is IL (MSIL/CIL) and what does the JIT do?

IL is the CPU-independent, stack-based intermediate code the C#/VB.NET compilers produce, stored with metadata in assemblies. At runtime the JIT compiles each method's IL to native machine code on its first call, optimised for the actual CPU; tiered compilation starts with quick code and re-compiles hot methods with full optimisation. ReadyToRun and Native AOT precompile to reduce startup time.


Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.

Next in the course: Assemblies, metadata, manifest, GAC, strong names and versioning →

Top comments (0)