Common Language Runtime in .NET

Common Language Runtime (CLR) in .NET:

In this article, I am going to discuss the Common Language Runtime (CLR) in C#.NET. Please read our previous article before proceeding to this article where we gave a brief introduction to the DOT NET Framework. At the end of this article, you will understand all about CLR in C# with examples. But before understanding CLR in .NET, let us first understand how a .NET Application compiled and run. 

How a .NET application compiled and run?

In order to understand how exactly a dot net application compiled and run, please have a look at the following image.

Common Language Runtime in .NET (CLR in C#.NET)

First, the developer has to write the code using any dot net supported programming languages such as C#, VB, J#, etc. Then the respective language compiler will compile the program into something called Intermediate language (IL) code. For example, if the programming language is C#, then the compiler is CSC and if the programming language is VB, then the compiler will be VBC. This Intermediate Language (IL) code is half compiled code i.e. partially compiled code and cannot be executed directly by the operating system. So, when you want to execute this IL code on your machine, the dot net framework provides something called CLR  or Common Language Runtime which takes the responsibility to execute your IL Code.

The CLR takes the IL (Intermediate Language) code and gives it to something called JIT (Just-in-Time) Compiler. The JIT compiler takes the IL code and reads each and every line of the IL code and converts it to machine-specific instructions (i.e. into binary format) which can be executed by the underlying operating system.

Intermediate Language (IL) Code:

IL code is a half compiled or partially compiled or CPU independent partially compiled code.

Why Partial Compiled Code or why not fully compiled Code?

As a developer, you may be thinking about why partially compiled code or why not fully compiled code. The reason is very simple. We don’t know in what kind of environment .NET Code will run (for example, Windows XP, Windows 7, Windows 10, Windows Server, etc.). In other words, we don’t know what operating system is going to run our application; we also don’t know the CPU configuration, machine configuration, security configuration, etc. So, the IL code is partially compiled and at runtime, this IL code is compiled to machine-specific instructions using environmental properties such as OS, CPU, Machine Configuration, etc.

Common Language Runtime (CLR):

CLR is the heart of the .NET Framework and it contains the following components.

  1. Security Manager
  2. JIT Compiler
  3. Memory Manager
  4. Garbage Collector
  5. Exception Manager
  6. Common Language Specification (CLS)
  7. Common Type System (CTS)

Let us discuss what each of these components does in detail.

Security Manager:

There are basically two components to manage security. They are as follows:

  1. CAS (Code Access Security)
  2. CV (Code Verification)

These two components are basically used to check the privileges of the current user that the user is allowed to access the assembly or not.  The CLR also sees what kind of rights or what kind of authorities this code has and whether it is safe to be executed by the operating system. So, basically, these types of checks are maintained by the Security Manager.

 JIT Compiler:

The JIT Compiler is responsible for Converting the MSIL code into native code that is executed by the machine. The native code is directly understandable by the system hardware. JIT compiles the code just before the execution and then saves this translation in memory.

Memory Manager:

The memory manager component allocates the necessary memory for the variables and objects that are to be used by the application.

Garbage Collector:

When a dot net application runs, lots of objects are created. At a given point in time, it is possible that we don’t need these objects. So, Garbage Collector is nothing but it is a small routine or you can say it’s a background process thread that runs and try to identify what objects are not being used currently by the application and de-allocates the memory of those objects.

Exception Manager:

The Exception Manager component of CLR redirects the processor to execute catch or finally blocks whenever an exception has occurred at runtime.

Common Type System (CTS):

The .NET Framework supports many programming languages such as C#, VB.NET, J#, etc. Every programming language has its own data types. One programming language cannot understand other programming language data types. But, there can be situations when you want to code in one language to be called in other languages. In order to ensure smooth communication between these languages, the most important thing is that they should have a common type system (CTS) which ensures that type defined in two different languages gets compiled to a common data type.

CLR will execute all programming language’s data types; this is possible because CLR will contain its own data types which are common to all programming languages.

At the time of compilation, all language-specific data types are converted into CLR’s data type. This data type system of CLR which is common to all programming languages of .NET is known as the Common Type System (CTS).

CLS (Common Language Specification):

CLS is a part of CLR. .NET supports many programming languages and every programming language has its own syntactical rules for writing the code which is known as language specification i.e. every programming language has its own language specification.

One programming language cannot understand other programming language syntactical rules (language specification). But CLR will execute all programming languages code. This is possible because CLR cannot understand any programming language specification rather CLR has its own language specification (syntactical rules) for its MSIL.

Every language compiler should follow this language specification of CLR at the time of compilation and should generate MSIL; CLR’s JIT compiler will generate native code from MSIL. This language specification of CLR is common for all programming languages code execution of .NET and is known as CLS.

In the next article, I am going to discuss the .NET Program Execution process. Here, in this article, I try to explain the Common Language Runtime (CLR) in C#.NET Framework. I hope you enjoy this CLR in C# article. 

6 thoughts on “Common Language Runtime in .NET”

  1. Pingback: Introduction to DOTNET Framework – Dot Net MVC Tutorials

  2. blank

    in the immediate previous article. IL Code is stated to be CPU dependent while in this CPU independent. Could you please clarify this?


Leave a Reply

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