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.
- Security Manager
- JIT Compiler
- Memory Manager
- Garbage Collector
- Exception Manager
- Common Language Specification (CLS)
- Common Type System (CTS)
Let us discuss each of these components in detail.
- This is the initial most component of CLR.
- The security manager first checks the privileges of the current user that the user is allowed to access the assembly or not.
- This security feature is meant for protecting the entire assembly and not to be accessed by unauthorized users.
- JIT Compiler is responsible for Converting the MSIL code into the native code that is executed by the machine.
- The native code is directly understandable by the system hardware.
- The memory manager component allocates the necessary memory for the variables and objects that are to be used by the application.
- This component de-allocates the unnecessary memory of the application after usage automatically.
- 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.
- 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
- Managed code
- Unmanaged code
Providing code execution support that has been written in other programming languages is known as language interoperability.
- 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.
- The code execution process is known as managed code execution.
- 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.
- 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.
- The code execution process is known as Un-managed code execution.
- 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.
- Examples for Un-Managed code are COM components.
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.