NET Common Language Runtime
NET Common Language Runtime
Suppose you have written a C# program and save it in a file which is known as the
Source Code.
Language specific compiler compiles the source code into the MSIL (Microsoft
Intermediate Language) which is also known as the CIL(Common
Intermediate Language) or IL(Intermediate Language) along with its
metadata. Metadata includes all the types, actual implementation of each
function of the program. MSIL is machine-independent code.
Now CLR comes into existence. CLR provides the services and runtime
environment to the MSIL code. Internally CLR includes the JIT(Just-In-Time)
compiler which converts the MSIL code to machine code which further
executed by CPU. CLR also uses the .NET Framework class libraries.
Metadata provides information about the programming language,
environment, version, and class libraries to the CLR by which CLR handles
the MSIL code. As CLR is common so it allows an instance of a class that
written in a different language to call a method of the class which written in
another language.
As the word specify, Common means CLR provides a common runtime or execution
environment as there are more than 60 .NET programming languages.
The Main components of CLR:
o Common type system
o Common language speciation
o Garbage Collector
o Just in Time Compiler
o Metadata and Assemblies
1. Managed Code: The MSIL code which is managed by the CLR is known as the
Managed Code. For managed code CLR provides three .NET facilities:
2. Unmanaged Code: Before .NET development, programming languages
like.COM Components & Win32 API do not generate the MSIL code. So these
are not managed by CLR rather managed by Operating System.
Every programming language has its own data type system, so CTS is responsible
for understanding all the data type systems of .NET programming languages and
converting them into CLR understandable format which will be a common format.
There are 2 Types of CTS that every .NET programming language have :
1. Value Types: Value Types will store the value directly into the memory
location. These types work with stack mechanisms only. CLR allows memory
for these at Compile Time.
2. Reference Types: Reference Types will contain a memory address of value
because the reference types won’t store the variable value directly in
memory. These types work with Heap mechanism. CLR allot memory for
these at Runtime.
Garbage Collector:
Metadata:
Assemblies:
Base Class Library Support: The Common Language Runtime provides support for
the base class library. The BCL contains multiple libraries that provide various
features such as Collections, I/O, XML, DataType definitions, etc. for the
multiple .NET programming languages.
Thread Support: The CLR provides thread support for managing the parallel
execution of multiple threads. The System.Threading class is used as the base
class for this.
COM Marshaller: Communication with the COM (Component Object Model)
component in the .NET application is provided using the COM marshaller. This
provides the COM interoperability support.
Type Checker: Type safety is provided by the type checker by using the Common
Type System (CTS) and the Common Language Specification (CLS) that are
provided in the CLR to verify the types that are used in an application.
Exception Manager: The exception manager in the CLR handles the exceptions
regardless of the .NET Language that created them. For a particular application,
the catch block of the exceptions are executed in case they occur and if there is no
catch block then the application is terminated.
Security Engine: The security engine in the CLR handles the security permissions
at various levels such as the code level, folder level, and machine level. This is
done using the various tools that are provided in the .NET framework.
Debug Engine: An application can be debugged during the run-time using the
debug engine. There are various ICorDebug interfaces that are used to track the
managed code of the application that is being debugged.
JIT Compiler: The JIT compiler in the CLR converts the Microsoft Intermediate
Language (MSIL) into the machine code that is specific to the computer
environment that the JIT compiler runs on. The compiled MSIL is stored so that it is
available for subsequent calls if required.
Code Manager: The code manager in CLR manages the code developed in the .NET
framework i.e. the managed code. The managed code is converted to intermediate
language by a language-specific compiler and then the intermediate language is
converted into the machine code by the Just-In-Time (JIT) compiler.
Garbage Collector: Automatic memory management is made possible using the
garbage collector in CLR. The garbage collector automatically releases the memory
space after it is no longer required so that it can be reallocated.
CLR Loader: Various modules, resources, assemblies, etc. are loaded by the CLR
loader. Also, this loader loads the modules on demand if they are actually required
so that the program initialization time is faster and the resources consumed are
lesser.