Common Language Runtime

Common Language Runtime (CLR):

In this article, I am going to discuss the Common Language Runtime (CLR) in detail. Please read our previous article before proceeding to this article where we gave a brief introduction to the DOT NET Framework. The CLR (Common Language Runtime) 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 each of these components in detail.

Security Manager:
  1. This is the initial most component of CLR.
  2. The security manager first checks the privileges of the current user that the user is allowed to access the assembly or not.
  3. This security feature is meant for protecting the entire assembly and not to be accessed by unauthorized users.
JIT Compiler:
  1. JIT Compiler is responsible for Converting the MSIL code into the native code that is executed by the machine.
  2. The native code is directly understandable by the system hardware.
Memory Manager:
  1. The memory manager component allocates the necessary memory for the variables and objects that are to be used by the application.
Garbage Collector:
  1. This component de-allocates the unnecessary memory of the application after usage automatically.
  2. In older languages such as C or C++, this kind of component is not available so that the programmer should free up the memory explicitly using some code.
Exception Manager:
  1. This component redirects the processor to execute catch or finally blocks whenever an exception has occurred at runtime.
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.

Role of CLS:

CLS is responsible to provide language interoperability. This is achieved in two ways

  1. Managed code
  2. Unmanaged code

Providing code execution support that has been written in other programming languages is known as language interoperability.

Managed Code:
  1. If the MSIL code is generated by the language compiler after compilation then it is called as managed code and this managed code is directly executed by CLR.
  2. The code execution process is known as managed code execution.
  3. CLR will provide all the facilities and features of .NET to the managed code execution like Language Interoperability, Automatic memory management, Exception handling mechanism, code access security, etc.
Unmanaged Code:
  1. Code that has written before the development of .NET for which MSIL is not available, that is also not executed by the CLR directly rather CLR redirects the code to OS for execution is known as unmanaged code.
  2. The code execution process is known as Un-managed code execution.
  3. CLR will not provide any facilities and features of .NET to the Un-managed code execution like Language Interoperability, Automatic memory management, Exception handling mechanism, code access security, etc.
  4. Examples for Un-Managed code are COM components.

Common Language Runtime Architecture

Note: Always managed code execution is faster than unmanaged code execution.

Common Type System (CTS):

.NET supports many programming languages. Every programming language has its own data types. One programming language cannot understand other programming language data types. But 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 the 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.

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 in DOTNET Framework. I hope you enjoy this article. 

Leave a Reply

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