Volume 1 · Chapter 9 — Modern C# and .NET Foundations
Modern .NET Runtime Features
9.1 Introduction
.NET has evolved from a Windows-only framework into a modern, cross-platform, open-source runtime built for cloud, containers, microservices, and high-performance workloads. For a senior engineer, the runtime and deployment features are not trivia: they directly determine startup time, memory footprint, scalability, and - because those drive container density and scaling speed - cloud cost. This chapter covers the runtime and publishing features that shape how efficiently modern .NET services run.
9.2 The Evolution of .NET
| Version | Key improvements |
|---|---|
| .NET Core | Cross-platform, open-source, modular runtime |
| .NET 5 | Unified platform replacing Framework and separate stacks |
| .NET 6 | LTS, minimal APIs, major performance gains |
| .NET 7 | Language and runtime optimizations |
| .NET 8+ | LTS, Native AOT improvements, cloud/container optimization |
9.3 ReadyToRun and Native AOT
These two features shift compilation from runtime to publish time.**ReadyToRun (R2R)**precompiles IL to native at publish time to reduce startup JIT cost, while keeping the JIT available to re-optimize hot paths.Native AOTcompiles the whole application to a native executable ahead of time and removes the JIT entirely, giving the fastest startup and smallest footprint - at the cost of some reflection and dynamic-code limitations.
flowchart TB IL["IL Assembly"] --> JIT["Plain JIT\n(compile at runtime)"] IL --> R2R["ReadyToRun\n(precompile startup path, keep JIT)"] Src["Application"] --> AOT["Native AOT\n(compile everything, no JIT)"] JIT --> Use1["Best: long-lived throughput services"] R2R --> Use2["Best: tame post-deploy warm-up"] AOT --> Use3["Best: serverless, CLI, tiny containers"]
9.4 Trimming and Single-File Deployment
Trimming removes unused code from a published app, shrinking container images and memory footprint (apps heavy on reflection need extra configuration so runtime-discovered code isn’t removed). Single-file publishing bundles the app, runtime, and dependencies into one executable, simplifying deployment for cloud and edge.
9.5 Container Optimization and Diagnostics
Because modern .NET services run in containers at scale, runtime efficiency directly affects infrastructure cost. Optimizations include appropriate (small) base images, trimming, reduced startup initialization, sane memory limits, and health checks. In production, EventPipe-based diagnostics (dotnet-counters,dotnet-trace,dotnet-dump) let you investigate running containers without a debugger.
When would you choose Native AOT, and what are its limitations?
I choose Native AOT when startup latency, memory footprint, and predictable performance matter more than the JIT’s adaptive optimization - typically serverless functions, CLI tools, and tightly-packed containers where cold-start time and image size are critical. Native AOT compiles the entire application to a self-contained native executable ahead of time and removes the JIT, so the app starts almost instantly (no JIT warm-up), uses less memory, and ships as a small, dependency-free binary - ideal where instances start and stop frequently or resources are constrained. The limitations are the trade-off: because everything is compiled ahead of time and the linker must statically determine what’s used, features that rely on runtime code generation or unbounded reflection are restricted or need explicit configuration - dynamic reflection, runtime IL emission, some serializers and DI patterns that scan/reflect at runtime, and dynamic assembly loading may not work without adaptation (which is why the ecosystem is moving to source generators that are AOT-compatible). There are also additional publishing considerations and platform-specific builds. So Native AOT is excellent for cold-start-sensitive, resource-constrained, reflection-light workloads, but for a long-running, throughput-oriented service that benefits from the JIT’s adaptive Tier-1 and PGO optimization - and freely uses reflection - plain JIT (optionally with ReadyToRun to tame warm-up) is usually the better choice. The decision comes down to the workload’s lifetime and startup sensitivity versus its need for runtime dynamism and peak steady-state throughput.
What is the difference between ReadyToRun and Native AOT?
Both move compilation from runtime toward publish time, but to different degrees and with different trade-offs. ReadyToRun precompiles the application’s IL into native code at publish time so that the startup path doesn’t need to be JIT-compiled on first run, reducing the post-startup warm-up cost -but it keeps the JIT and the full runtime available, so hot methods can still be re-optimized at runtime (Tier 1/PGO), reflection and dynamic code work normally, and it’s a relatively low-friction option; the cost is larger binaries (they contain both IL and precompiled native code). Native AOT goes all the way: it compiles the entire application ahead of time into a self-contained native executable andremoves the JIT and much of the runtime infrastructure entirely, giving the fastest startup and smallest memory footprint, but at the cost of losing runtime JIT optimization and imposing limitations on reflection and dynamic code generation. So ReadyToRun is a middle ground - faster startup while retaining full runtime flexibility and adaptive optimization - suited to reducing deployment warm-up on services that still want the JIT; Native AOT is the aggressive option for cold-start-critical, resource-constrained, reflection-light workloads (serverless, CLI, tiny containers) where you accept the limitations to get instant startup and a tiny footprint. In short: R2R precompiles but keeps the JIT (flexible, larger); AOT compiles everything and drops the JIT (fastest/smallest, restricted).
How do runtime and deployment choices affect cloud cost?
They affect cost directly through three levers - startup time, memory footprint, and CPU efficiency - because in the cloud you pay for compute resources and time. Startup time matters because it governs how fast you can scale out to meet demand (slow-starting instances mean you must over-provision to absorb spikes, or users see latency during scale-up) and, in serverless, cold-start time is literally billed latency; ReadyToRun and Native AOT reduce it. Memory footprint matters because it determines container density - how many instances fit on a node - and thus how much infrastructure you need; trimming, Native AOT, and low-allocation code shrink it, letting you pack more instances per machine and run smaller, cheaper instance sizes. CPU efficiency (from good algorithms, low allocations that reduce GC overhead, and the JIT’s optimizations) determines how much work each core does, so more efficient code needs fewer instances for the same load. Concretely: a service that starts fast and uses little memory can scale quickly, pack densely, use smaller instances, and in serverless bill less - all reducing cost; a slow-starting, memory-heavy service forces over-provisioning and larger instances. So I treat runtime and deployment choices - AOT/R2R for startup, trimming and low-allocation design for memory, efficient code for CPU, and right-sized base images - as cost-optimization decisions, matching them to the workload (serverless/bursty favors AOT and small footprint; steady high-throughput favors JIT efficiency), because at scale across many containers these choices translate directly into the cloud bill.
9.6 Production Scenario
A microservice platform running hundreds of containers experiences high startup latency during deployments, which slows scaling and forces over-provisioning. Startup profiling shows heavy JIT warm-up and initialization. The team enables ReadyToRun, reduces startup initialization work, and optimizes the container base image - cutting startup time, improving scaling speed and container density, and lowering infrastructure cost.
Chapter Revision Notes
- Modern .NET is cross-platform and cloud/container-optimized.
- ReadyToRun precompiles the startup path but keeps the JIT; Native AOT removes the JIT for fastest startup.
- Trimming and single-file publishing shrink footprint and simplify deployment.
- Runtime efficiency (startup, memory, CPU) directly drives cloud cost.
- EventPipe diagnostics investigate running containers without a debugger.