What is CIL (Common Intermediate Language), and what role does it play between source code and execution?
Short answer
CIL is the CPU-independent, low-level instruction set that every .NET compiler (csc for C#, the F# compiler, etc.) produces instead of native machine code -- this intermediate step is what enables both cross-platform execution (the CLR's JIT compiler translates CIL to whatever…
Advertisement
AModel answer
CIL is the CPU-independent, low-level instruction set that every .NET compiler (csc for C#, the F# compiler, etc.) produces instead of native machine code -- this intermediate step is what enables both cross-platform execution (the CLR's JIT compiler translates CIL to whatever machine code the current OS/CPU needs) and language interoperability (a C# class and an F# class both compile down to the same CIL representation, so they can call each other directly without any bridging layer).
// You can inspect a compiled assembly's CIL using a tool like ildasm or dotnet-ildasm
// A simple method like this:
public int Add(int a, int b) => a + b;
// compiles to CIL roughly resembling:
// ldarg.1
// ldarg.2
// add
// retReal-world example
A library written in F# exposing a public class is consumed directly from a C# project with zero special glue code, only possible because both languages' compilers target the identical CIL format that the CLR understands.
Likely follow-ups
- Why can't CIL be executed directly by a CPU without the JIT step?
- How does CIL enable tools like decompilers to reconstruct readable source from a compiled DLL?
Related topics