Common Language Runtime

In this article, we’ll discuss the CLR i.e., Common Language Runtime in .NET Framework. Please check out the brief of DOT NET Framework in our last article. I hope after reading this article, you will get clear understanding about CLR in C# with examples. But before understanding CLR in .NET, let us first understand how .NET application is compiled and run. 

How is a .NET application compiled and run?

You’ll write code in C# or VB and then its respective compiler will compile code and generate MSIL. Next CLR will convert MSIL to Native code and then It’ll finally execute. In order to understand how .NET application is compiled and run, please have a look at the following image.

Common Language Runtime: should read

What is a CLR?

CLR is Common Language Runtime. If we say that it forms the heart of the .NET Framework, then that will not be wrong. All Languages have their own runtime and this runtime take care of all code execution. As .NET has CLR similarly all other languages are also having their own Runtime like Java has Java Virtual Machine and VC++ has MSCRT40.DLL etc.

Following table explains the CLR version present in respective .NET Versions:

.NET VersionCLR Version
1.01.0
1.11.1
2.02.0
3.02.0
3.52.0
4.04.0
4.54.0
4.64.0
4.74.0
4.84.0
4.8.1 (latest till now)4.0

Following are the functions / responsibilities of CLR.

  • Garbage collection
  • It converts the program into native code.
  • Handles Exceptions
  • Provides type-safety and security.
  • Memory management
  • Improved performance
  • Language independent
  • Platform independent

DOT NET CLR Structure

Following are the components of “Common Language Runtime”.  

Common Language Runtime: should read

Please check the brief introduction of each given in above picture:

Security Engine:

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

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

CAS comes into picture when we have to check the privileges and restrict the code from executing DLL or some specific of code. Security Manager in .NET Application manage all these types of security. CLR allows only those operations for which the code has permission to perform. CAS can do followings:

  • Restrict what your code can do and cannot do.
  • Restrict what assembly or code can be access or not.

Following are the command of CAS:

  • Run ‘caspol -lg‘ in Visual studio command to see all available CAS objects.
  • Run ‘caspol -ag 11.9 -site www.abc.com FullTrust‘ used to add new CAS object with full trust.
  • Run ‘caspol -cg 11.9 -site www.abc.com FullTrust‘ used to alter CAS object.
  • Run ‘caspol -s off‘ used to turn off CAS.

JIT Compiler:

The JIT (Just-In-Time) Compiler is responsible for Converting the MSIL code into native code (Machine Code or Binary code). Native code is machine dependent code and runs on that machine only. JIT compiles the code just before the execution and it is of 3 types.

  • PRE JIT compiler-It’ll compile whole code into ‘Native code’ in single go.
  • ECO JIT complier– compile only specific methods called at runtime.
  • Normal JIT compiler– compile the specific code called at runtime and add it in CACHE.

Memory Manager:

Memory manager is a component of CLR, and it is used to allocates the necessary memory to objects and variables those are the part of program.

Common Type System (CTS):

CTS is the common type system which explains how types are declare, used and managed. As we know that .NET Framework supports many programming languages such as C#, VB, F#, C++ etc. Every programming language has its own data types. You can used C# and VB in your project (in different class lib) and further use these libraries/ DLLs in your application. Here CTS will helps managing types in different language hence we can say that for smoothly communication of (two or more) programming languages CLR has CTS.

Example: In VB you have “Integer”, C# has int32 and in C++ you have “long” these datatypes are not compatible so communication between them is very complicated. Here comes CTS which converts “Integer” datatype in VB6, “int” datatype in C++ and int32 of C# it to System.int32 which is datatype of CTS and which make communication feasible.

Common Language Specification (CLS):

CLS is a specification that defines the rules to support language integration. CLS is nothing but guidelines that language have to follow so that it can communicate with other .NET languages. CLS is the subset of CTS. It was dream of Microsoft to unite all languages under one umbrella so that developers could use any language and that can be build and run under .NET framework.

Garbage Collector:

When a dot net application runs, lots of objects/ variables are created. With the passage of time at certain point, it is possible that some of those objects are not getting used by the application. So, to release this memory Garbage Collector comes into picture. GC is a Background Process Thread that runs periodically and try to identify all objects those are not in use since long time further it will d e-allocates the memory of those objects.

Exception Manager:

The Exception Manger is an important component of CLR. When a runtime error is occurred in code Exception Manager takes the control and redirects the control to execute ‘catch’ or ‘finally’ blocks so that error can be handled and logged.

You can check out more similar article HERE. For more depth you can also check the same on Microsoft learn.