How .NET Works: CLR, IL, JIT and Assemblies
From Code to Running Program
- You write C# source code (
.csfiles). - The C# compiler (Roslyn) turns it into IL (Intermediate Language) stored in an assembly (
.dllor.exe). - When the program starts, the CLR (Common Language Runtime) loads the assembly.
- The JIT compiler converts IL into machine code for your CPU, just in time.
- The CPU runs the machine code.
Simple example: IL is like a recipe written in a universal language. The JIT is a cook who translates it for the local kitchen at the moment of cooking. That is why the same assembly runs on Windows, Mac and Linux.
The Main Parts
| Part | What it does |
|---|---|
| C# compiler (Roslyn) | Checks your code and produces IL |
| CLR | The runtime: loads code, manages memory, handles exceptions and security |
| JIT | Compiles IL to fast machine code when needed |
| Garbage Collector (GC) | Frees memory of objects you no longer use |
| BCL (Base Class Library) | Built-in classes: strings, collections, files, networking, JSON |
| Assembly | The compiled unit: a .dll library or .exe program |
Managed Code and the Garbage Collector
In C# you create objects with new and never free them manually. The garbage collector finds objects that nothing refers to any more and releases their memory. This avoids a large class of memory bugs common in C and C++.
csharp
void Work()
{
var list = new List<int> { 1, 2, 3 }; // memory allocated
Console.WriteLine(list.Count);
} // after this method, 'list' is unreachable and the GC can clean it up
Remember: The GC frees memory only. Things like files, database connections and sockets must be released by you, using
using and IDisposable (we cover it in a later lesson).
Ways to Publish an App
| Mode | Command | Notes |
|---|---|---|
| Framework-dependent | dotnet publish -c Release | Small. The target computer must have the .NET runtime. |
| Self-contained | dotnet publish -c Release --self-contained -r win-x64 | Includes the runtime. Larger but runs anywhere without install. |
| Native AOT | <PublishAot>true</PublishAot> | Compiled ahead of time. Fast startup, no JIT, with some limits. |
Value Types and Reference Types (Preview)
| Value type | Reference type | |
|---|---|---|
| Examples | int, double, bool, struct | string, class, arrays |
| Stored | Directly in the variable | Object on the heap, variable holds a reference |
| Copy | Copies the value | Copies the reference |
We use this idea throughout the course, and explain it again with examples in the OOP section.
Conclusion
C# compiles to IL, the CLR and JIT run it as machine code and the garbage collector manages memory. The BCL gives you thousands of ready classes.
Quick Summary: C# > IL > JIT > machine code. GC frees memory. BCL is the standard library.

