CLR

The .NET Common Language Runtime (CLR) functions as a runtime platform responsible for overseeing and running code developed in various .NET programming languages. It serves as the essential virtual machine element within the .NET framework. The compiler specific to each language within the framework translates the source code of applications created with .NET-compatible languages into MSIL (Microsoft Intermediate Language), an intermediate language unique to CLR. This MSIL code is not tied to any particular platform, similar to Java's bytecode. As part of the compilation process, metadata is also produced alongside the MSIL code and is saved in a file termed the Manifest file. This metadata primarily contains details regarding the members and types essential for CLR to execute the MSIL code effectively. Within CLR, a just-in-time compiler module converts the MSIL code into the native code of the underlying machine, which is platform-specific. CLR undertakes various crucial tasks such as memory management, threading, exception handling, code execution, safety mechanisms, verification, and compilation processes.

The upcoming illustration demonstrates the transformation of source code into machine code.

The CSS styles for the diagram elements have been defined as follows:

  • The background of the diagram has a linear gradient with specific colors, rounded corners, padding, and margin.
  • The title of the diagram is centered with a specific color, font size, weight, and bottom margin.
  • The core of the diagram has a semi-transparent background with rounded corners and padding.
  • The title of the core is centered with a specific color, font weight, and bottom margin.
  • The components of the diagram are displayed in a grid layout with specific column sizes and gaps.
  • Each component has a slightly transparent background with rounded corners, padding, and center-aligned text.
  • The icon within each component has a specific size and bottom margin.
  • The name of each component is in white color with a particular font weight, size, and bottom margin.
  • The description of each component is in a specific color and font size.

The diagram above transforms code into machine code, which can be directly executed by the CPU.

The main components of CLR are:

  • Common type system
  • Common language speciation
  • Garbage Collector
  • Just in Time Compiler
  • Metadata and Assemblies

The styles defined for the diagram include a gradient background, rounded corners, padding, and margins to enhance its visual appeal. The title of the diagram is centered with a specific font size, color, and weight to make it stand out. The core section has a slightly transparent background with rounded corners and padding for a neat look. Components are arranged in a grid layout with specific column sizing and gaps between them. Each component has a semi-transparent background, rounded borders, padding, and text styling for a cohesive design.

1. Common type system:

CTS presents recommendations for declaring, utilizing, and overseeing data types dynamically. It facilitates interoperability across different programming languages. For instance, VB.NET incorporates a data type named integer, while C# utilizes int for handling integer values. Following compilation, both data types rely on Int32. Therefore, CTS facilitates the utilization of data types via managed code, fostering the development of language-agnostic code with a unified type system.

It presents two classifications of Types.

  • Value Type: A value type stores information in memory allocated on the stack or within a structure. This group of Type retains the data directly. When one variable's value is duplicated to another, both variables hold data separately. It can be of built-in types, user-defined types, or enumeration types. Primitive data types like numeric, Boolean, char, and date are examples of built-in types. User-defined types are created by users in the source code. An enumeration represents a collection of enumerated values displayed by labels but saved as a numeric type.
  • Reference Type: A Reference type retains a reference to the value of a memory address and is allocated on the heap. Heap memory is utilized for dynamic memory allocation. Reference Type does not directly hold real data but keeps the address of the data. When a reference type object is created, it duplicates the address and not the actual data. Hence, two variables will point to the same data. If the data of one Reference Type object is modified, the same change is seen in the other object. Reference types can be self-describing types, pointer types, or interference types. Self-describing types include string, array, and class types that hold metadata about themselves.

The CSS classes defined in the code snippet create a styled diagram interface for showcasing components. The design includes a gradient background, rounded borders, specific padding, and margins to enhance the visual presentation. Each component within the diagram is enclosed within its own styled container, featuring different text alignments, font colors, sizes, and weights to differentiate various sections. The grid layout ensures a structured arrangement of components, maintaining consistent spacing between them.

2. Common Language Specification (CLS):

The Common Language Specification (CLS) comprises a collection of guidelines that must be adhered to by all languages supported by the .NET framework. These universal regulations simplify the process of integrating languages and facilitate cross-language inheritance and debugging. While each .NET-supported language possesses its unique syntax regulations, adherence to the CLS guarantees seamless interoperability across applications developed in various .NET languages.

3. Garbage Collection:

The Garbage Collector, a vital part of the Common Language Runtime (CLR), functions as an automated memory handler. Its role involves the efficient allocation of memory based on the program's needs. Specifically, it assigns heap memory to objects and retrieves memory from unused objects for potential future allocations. Additionally, it upholds object safety by preventing one object from accessing another object's data.

4. Just in Time (JIT) Compiler:

The Just-In-Time (JIT) Compiler plays a crucial role within the Common Language Runtime (CLR) environment. Its primary function is to transform the Microsoft Intermediate Language (MSIL) code into machine-specific native code. The compilation of a .NET program can occur explicitly or implicitly, depending on the scenario. Explicit compilation involves a developer or programmer triggering a specific compiler for compiling the program. Conversely, implicit compilation entails a two-step process. Initially, the source code is compiled into MSIL during the first compilation phase. Subsequently, the MSIL code undergoes conversion into native code in the second compilation stage, known as JIT compilation.

Within JIT compilation, there exist three distinct types of JIT compilers: Pre JIT, Econo JIT, and Normal JIT. The Pre JIT Compiler performs a comprehensive compilation of the entire MSIL code into native code before execution. On the other hand, the Econo JIT Compiler selectively compiles the essential parts of the MSIL code required during execution, discarding the unnecessary segments. Similarly, the Normal JIT Compiler also compiles only the pertinent portions of the MSIL code necessary for execution but stores them in cache for future utilization. This caching mechanism eliminates the need for recompilation of previously utilized sections, as they are already stored in the cache memory.

5. Metadata:

Metadata is a crucial set of binary data that describes a program, typically stored in a CLR Portable Executable (PE) file alongside MSIL code or in the system memory. When MSIL code runs, the metadata is loaded into memory to correctly interpret classes and other information used within the code. This metadata plays a key role in enabling code implementation in a language-agnostic way and facilitating language interoperability.

6. Assemblies:

An assembly serves as a foundational element of organizing physical code. It comprises the assembly manifest, metadata, MSIL code, and a collection of resources such as image files. Additionally, it is recognized as a fundamental deployment unit, essential for version control, promoting reuse, managing security permissions, and more.

.NET CLR Functions

Following are the functions of the CLR.

  • It converts the program into native code.
  • Handles Exceptions
  • Provides type-safety
  • Memory management
  • Provides security
  • Improved performance
  • Language independent
  • Platform independent
  • Garbage collection
  • Provides language features such as inheritance, interfaces, and overloading for object-oriented programs.

The code executed within the Common Language Runtime (CLR) is referred to as managed code, while the code that operates outside the CLR is known as unmanaged code. Additionally, the CLR offers an Interoperability layer that facilitates the interaction between both managed and unmanaged codes.

1. Managed code:

Any programming language designed to run within the .NET framework is considered managed code. Managed code operates within the Common Language Runtime (CLR), which takes care of various tasks such as memory management, security enforcement, supporting cross-language debugging, and more. The diagram below illustrates the workflow of managed code:

The CSS code provided defines styles for a color diagram. The diagram has a gradient background, rounded corners, padding, and margins. The title is centered with a specific color, font size, and weight. The core section has a semi-transparent background with rounded corners and padding. Each component is displayed in a grid layout with defined column sizes and gaps. Components have a slightly transparent background, rounded corners, padding, and center-aligned text. Each component includes an icon, name in white color with a specific font weight and size, and a description in a light gray color.

2. Unmanaged code:

Code that is created outside of the .NET framework is referred to as unmanaged code. Programs that do not operate within the Common Language Runtime (CLR) are categorized as unmanaged. Languages like C++ are commonly employed for developing such applications, which may involve tasks requiring direct interaction with the operating system at a lower level. Compatibility with legacy technologies like VB, ASP, and COM are instances of unmanaged code usage. To execute unmanaged code, wrapper classes are utilized. The sequence of the unmanaged code process is illustrated below:

The styling for the diagram includes a background with a linear gradient, border radius, padding, and margin properties. The title of the diagram is centered with a specific color, font size, and weight. The core of the diagram has a background with transparency, a certain border radius, and padding. Each component in the diagram is displayed in a grid format with specified column template and gap size. Components have a background with partial transparency, border radius, padding, and text alignment. Each component includes an icon, a name with specific styling, and a description with a defined color and font size.

.NET CLR Versions

The Common Language Runtime periodically updates itself to enhance performance.

.NET version CLR version
1.0 1.0
1.1 1.1
2.0 2.0
3.0 2.0
3.5 2.0
4 4
4.5 4
4.6 4
4.6 4

.NET CLR Structure

Below is the component layout of Common Language Runtime.

The styling for the diagram includes a gradient background, rounded corners, padding, and margin to enhance its visual appeal. The title of the diagram is centered with a specific color, font size, and weight for emphasis. The core section has a semi-transparent background with rounded edges and padding for a distinct look. Components are arranged in a grid layout with specified column sizes and a defined gap between them. Each component has a slightly transparent background, rounded corners, padding, and text alignment for consistency. Icons and text within the components are styled with different font sizes, colors, and weights to provide visual hierarchy and information clarity.

Base Class Library Support

It is a library of classes that provides support for classes within the .NET application.

Thread Support

It oversees the simultaneous execution of a multi-threaded application.

COM Marshaler

It facilitates interaction between the COM objects and the software application.

Security Engine

It enforces security restrictions.

Debug Engine

It enables you to troubleshoot various types of applications.

Type Checker

It examines the data types utilized within the software and confirms their adherence to the guidelines established by the CLR.

Code Manager

It manages code at execution runtime.

Garbage Collector

It frees up the memory that is not in use and assigns it to a different application.

Exception Handler

It manages the exception during runtime to prevent application crashes.

ClassLoader

It is used to load all classes at run time.

Input Required

This code uses input(). Please provide values below: