Compilation and Execution Plan of .NET

Compilation and Execution plan of .NET is a two-step process. In other words, the source code developed in the .NET environment is not translated directly to the machine code. Rather, it is first converted into some kind of intermediate-level code. To emphasize further, The .NET compiler compiles the source code to an intermediate level code. We call this intermediate-level code as IL (Intermediate Language) or MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language) Code.

Therefore, compiling a .NET program doesn’t result in creating an executable (.EXE) file or Dynamic Link Library (.DLL) file. In other words, we can say that compiling a .NET program results in generating MSIL code.

Now, this MSIL code is further processed by Common Language Runtime (CLR). It is the CLR that converts the MSIL code into the native code which is ready to run on the machine.

The compilation process results in the generation of a file. This file is referred to as Assembly. The assembly contains the MSIL code.

When CLR gets the MSIL code and converts it into the machine code, it performs several tasks.

  1. First of all, it performs a security check on the assembly. If the assembly passes the security check, only then CLR converts it into the machine code.
  2. The program requires space in memory. Hence, CLR allocates the space for assembly. It runs the Garbage Collector (GC). The Garbage Collector allocates memory to objects.
  3. CLR has a module called JIT (Just In Time) Compiler. The JIT compiler converts MSIL Code into native code. However, the JIT compiler creates the native code only when it is required. Hence, it converts only a part of the MSIL code into native code.
  4. CLR also ensures Type Safety. If CLR detects any violation in Type Safety, it generates an exception.
  5. In order to improve performance, JIT also caches the code in memory method-wise and utilizes the cached code when a call to that method occurs.

The native code that CLR generates, is then executed by the Operating System. To clarify the Compilation and Execution Plan of .NET, see the figure

Compilation and Execution Plan of .NET
Compilation and Execution Plan of .NET

To summarize, the compilation and execution plan of .NET involves two entities – the language compiler and the Common Language Runtime (CLR).

Important Points to Remember

(1) A .NET program runs in two steps – first, the language compiler compiles the source code into MSIL code, and then CLR converts MSIL codes into machine code. (2) Compiling a .NET program generates an Assembly. (3) The CLR takes MSIL code as input and sends it to the JIT compiler, which converts MSIL code into machine code as needed. (4) While, creating the machine code, CLR performs various tasks such as Garbage Collection and Type Safety.

Further Reading

Assemblies in .NET

Leave a Comment

Your email address will not be published. Required fields are marked *