Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท .NET, Tools and Concepts

The .NET runtime

How the .NET runtime runs C# code: intermediate language, the CLR, the JIT compiler and tiered compilation, the base class library, and how to inspect them.

The .NET runtime is the program that actually runs your C# code. The C# compiler does not produce machine code. It produces intermediate language (IL) plus metadata, packed into an assembly (.dll or .exe). When you start the application, the runtime, officially called the Common Language Runtime (CLR), loads the assembly, verifies it, and compiles the IL to machine code for the current processor while the program runs. That compiler is the JIT (just-in-time compiler).

The runtime also provides the services that make managed code convenient: the garbage collector, type safety, exceptions, threading, and the base class library (BCL) of types such as List<T>, File and Task. Together with the SDK tools they form ".NET".

TEXT
C# source --(Roslyn)--> IL + metadata (.dll) --(CLR loads, JIT)--> machine code --> CPU

Try it

C# Asking the runtime about itself

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# The JIT compiles a method the first time it runs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

How it works

Intermediate language

IL is a stack-based, CPU-independent instruction set. Compiling to IL first is why one .dll runs on Windows, Linux and macOS, and on x64 and Arm. It also means the runtime has a complete description of every type (metadata), which is what makes reflection, serialization and debuggers possible.

The JIT and tiered compilation

The JIT compiles a method the first time it is called. Since .NET Core 3.0, tiered compilation is the default: a method starts at tier 0, compiled quickly with few optimizations so the program starts fast. The runtime counts calls; methods that run often (about 30 calls, then a short delay) are recompiled at tier 1 with full optimization. Methods with hot loops use on-stack replacement to jump into optimized code in the middle of a loop. Dynamic PGO (on by default since .NET 8) instruments tier 0 code and uses what it learns to optimize tier 1: inlining, devirtualizing calls and arranging hot and cold paths.

Related options:

Setting Effect
TieredCompilation Turn tiering off to JIT everything at full optimization (slower start).
TieredPGO Turn profile-guided optimization on or off.
ReadyToRun Precompile assemblies to native code at publish time to shorten startup; the JIT still re-optimizes hot methods.
Native AOT Compile everything ahead of time into a standalone native executable, with no JIT at all, and with limits on reflection and dynamic code.

What else the runtime does

  • Type loading and safety: the runtime checks casts, array bounds and null dereferences, and throws exceptions instead of corrupting memory.
  • Memory management: the garbage collector allocates and reclaims heap objects.
  • Threading: the thread pool, synchronization primitives and the Task machinery sit on the runtime's services.
  • Interop: P/Invoke and COM interop call native code.
  • Diagnostics: event tracing, counters, dumps and debugging hooks.

Runtime, SDK and frameworks

The SDK (dotnet command, compilers, MSBuild) builds code. The runtime (Microsoft.NETCore.App) runs it. ASP.NET Core (Microsoft.AspNetCore.App) is a shared framework with web types. A framework-dependent app needs the matching runtime on the machine; a self-contained app carries its own copy.

Usage notes

  • The first call is slower than later calls. Benchmarks must warm up code first; a single timing run measures mostly tier 0 and JIT cost.
  • IL is not the final speed. Compare optimized (tier 1) behavior only; use BenchmarkDotNet for measurement.
  • [MethodImpl(MethodImplOptions.NoInlining)] keeps the JIT from merging a small method into its caller, which is useful for experiments and stack traces. AggressiveInlining and AggressiveOptimization are hints; use them only with measurements.
  • Debug and Release differ. Release builds produce IL with optimizations (inlined constants, no extra nops), so locals can be collected earlier and debugging is harder.
  • Platform differences: IntPtr.Size is 8 on 64-bit processes, 4 on 32-bit. Never assume it.
  • The runtime version is not the language version: Environment.Version tells you the first, the compiler the second.