Managed execution and allocation
Managed execution and allocation
In a conventional .NET build, the C# compiler emits Common Intermediate Language (CIL) and metadata in an assembly. CoreCLR commonly converts CIL to native instructions through just-in-time compilation. ReadyToRun and Native AOT use different preparation strategies. The runtime manages type loading, exceptions, and garbage collection; it does not mean every operation allocates a new object.
Proposed baseline: .NET 10 / C# 14 (SDK 10.0.404 inherited). Ordinary CoreCLR SDK console build, not Native AOT. No additional packages. Outputs are reasoned, not executed.
Inspect a method signature and IL availability
Reflection exposes metadata and, for this ordinary non-AOT build, the method body. Do not compare byte sequences across compiler versions.
using System;var method = typeof(Price).GetMethod(nameof(Price.Total))!;Console.WriteLine(method.ReturnType.Name);Console.WriteLine(method.GetMethodBody()?.GetILAsByteArray() is { Length: > 0 });Console.WriteLine(Price.Total(4, 3));public static class Price { public static int Total(int quantity, int unit) => quantity * unit; }The method body contains IL, not a disassembly of native instructions. The ordinary SDK build preserves the method body; trimming or AOT may limit reflection.
Expected observation (reasoned, not executed):
Int32True12Compare struct copies with shared references
A value type assignment copies the value, while a reference type assignment copies a reference.
using System;var a = new Point { X = 2 };var b = a;b.X = 7;var first = new Holder { X = 2 };var second = first;second.X = 7;Console.WriteLine($"{a.X},{b.X}");Console.WriteLine($"{first.X},{second.X}");public struct Point { public int X; }public class Holder { public int X; }These results are language semantics, not proof of where objects or variables reside. A struct can be stored inline in a heap object, an array, a stack slot or a register.
Expected observation (reasoned, not executed):
2,77,7Box a value and unbox it
Conversion from int to object boxes the integer; the box contains a copy of the value.
using System;int count = 6;object boxed = count;count = 9;Console.WriteLine((int)boxed);Console.WriteLine(count);The boxed value is independent of later assignments to count. The implementation can optimize allocation if observable behavior is preserved.
Expected observation (reasoned, not executed):
69Common mistakes and edge cases
Do not teach that value types always live on the stack or that every class allocation is visible in an allocation counter. JIT optimizations, boxing, closures, arrays, static fields and async state machines complicate the picture. A language guarantee is different from an implementation observation. Use a standard SDK build for the IL example; Native AOT is a separate publishing mode.
Practice and recap
Trace Price.Total from source through build and ordinary runtime execution. Explain why the boxed count remains 6.
Hint: Separate metadata from native code.
Learning objective: Distinguish source, IL, JIT and native execution. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Managed execution and allocation — knowledge check
Apply the lesson concepts to the following scenarios.
What does a conventional C# build usually emit?
What does the JIT produce?
Where can a struct field reside?
What does boxing an int preserve?
What is IL inspection not evidence of?