What is CIL and CLR?

What is CIL and CLR?

Common Intermediate Language (CIL) is a set of instructions that are platform independent and are generated by the language-specific compiler from the source code. The CIL also known as The Microsoft Intermediate Language (MSIL).

MSIL is platform independent and consequently, it can be executed on any of the Common Language Infrastructure supported environments such as the Windows .NET runtime.

The MSIL is converted into a particular computer environment specific machine code by the Just In Time compiler. This is done before the MSIL can be executed. Also, the MSIL is converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL as required rather than the whole of it.

Execution process in Common Language Runtime (CLR): The execution process that includes the creation of the CIL and the conversion of the MSIL into machine code by the JIT compiler is given as follows:

The source code is converted into the CIL by a language-specific compiler in the compile time of the CLR. Also, along with the CIL, metadata is also produced in the compilation. The metadata contains information such as the definition and signature of the types in the code, runtime information, etc.

  • A Common Language Infrastructure (CLI) assembly is created by assembling the MSIL. This assembly is basically a compiled code library that is used for security, deployment, versioning, etc. and it is of two types i.e. process assembly (EXE) and library assembly (DLL).
  • The JIT compiler then converts the Microsoft Intermediate Language(MSIL) into the machine code that is specific to the computer environment that the JIT compiler runs on. The MSIL is converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL as required rather than the whole of it.
  • The machine code obtained using the JIT compiler is then executed by the processor of the computer.

What Is CLR?

Common Language Runtime (CLR) manages the execution of .NET programs. The just-in-time compiler converts the compiled code into machine instructions. This is what the computer executes.

The services provided by CLR include memory management, exception handling, type safety, etc.

Let us see the features of Common Language Runtime (CLR) in C#:

Components

Components in other languages can be easily worked upon with CLR.

Threading

The CLR provides support for threads to create multithreaded applications.

Class Library Support

It has built-in types and libraries for assemblies, threading, memory management, etc.

Debugging

CLR makes code debugging easier.

Garbage Collection

It provides automatic garbage collection in C#.

The code that runs under the Common Language Runtime is termed as the Managed Code. In other words, you can say that CLR provides a managed execution environment for the .NET programs by improving the security, including the cross language integration and a rich set of class libraries, etc. CLR is present in every .NET framework version. Below table illustrate the CLR version in .NET framework.

Below diagram illustrate how CLR is associated with the operating system/hardware along with the class libraries. Here, the runtime is actually CLR.