Overview
Intermediate Language (IL) is the low‑level set of instructions understood by the .NET Common Language Runtime (CLR). All source languages compile to IL, which is then JIT‑compiled or pre‑compiled to native code at runtime.
IL Syntax
The IL language uses a simple stack‑based model. Each instruction operates on the evaluation stack or on local variables and arguments.
.assembly SampleApp {}
.module SampleApp.dll
.type public sealed SampleApp
{
.method public hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 8
ldstr "Hello, World!"
call void [System.Console]System.Console::WriteLine(string)
ret
}
}
Example: Simple Calculator
The following IL program demonstrates basic arithmetic operations:
.assembly Calc {}
.module Calc.dll
.type public sealed Calc
{
.method public static int32 Add(int32 a, int32 b) cil managed
{
ldarg.0
ldarg.1
add
ret
}
.method public static void Main() cil managed
{
.entrypoint
.maxstack 2
ldc.i4.s 10
ldc.i4.s 20
call int32 Calc::Add(int32, int32)
call void [System.Console]System.Console::WriteLine(int32)
ret
}
}
Metadata & Tokens
IL refers to types, methods, fields, and other entities using metadata tokens. Tokens are encoded as 4‑byte values with a high‑order byte indicating the table type (e.g., 0x06
for MethodDef) and the remaining bytes as an index.
- TypeRef – References to types defined in other assemblies.
- MethodDef – Definitions of methods within the current assembly.
- FieldDef – Definitions of fields.