Open In App

Common Language Runtime (CLR) in C#

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.

When a C# program is compiled, the resulting executable code is in an intermediate language called Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL). This code is not machine-specific, and it can run on any platform that has the CLR installed. When the CIL code is executed, the CLR compiles it into machine code that can be executed by the processor.

Working of CLR

1. Compilation and Execution:

  • When we write a C# program, it gets compiled into Intermediate Language (IL) code, which is platform-independent.
  • The CLR then uses a Just-In-Time (JIT) compiler to convert the IL code into machine-specific code while the program runs.

2. Services Provided by CLR:

  • CLR handles automatic memory management through Garbage Collection, preventing memory leaks.
  • It ensures that data types are used correctly and safely.
  • The CLR checks the IL code for security risks before running it.

3. Cross-Language Integration:

CLR allows code from different .NET languages (C#, VB.NET, F#) to work together seamlessly through the Common Type System (CTS).

Key Components of CLR

As the word specify, Common means CLR provides a common runtime or execution environment as there are more than 60 .NET programming languages. 

  • Common Language Specification (CLS): It defines common rules so that code written in different languages can interoperate.
    • 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: 
    • 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.
  • JIT Compiler: It converts IL code into machine code specific to the system at runtime, optimizing execution.
  • Garbage Collector: It automatically manages memory by freeing unused objects, reducing the need for manual memory management.
  • Common Type System (CTS): This ensures that different data types across languages are understood by CLR and can work together. There are 2 Types of CTS that every .NET programming language have:
    • 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.
    • 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.

Below table illustrate the CLR version in .NET framework. 

CLR Versions .NET Framework Versions
1.0 1.0
1.1 1.1
2.0 2.0
2.0 3.0
2.0 3.5
4 4
4 4.5 (also 4.5.1 & 4.5.2)
4 4.6 (also 4.6.1 & 4.6.2)
4 4.7 (also 4.7.1 & 4.7.2)

4

4.8 (also 4.8.1)

6

8.0

9.0

Below diagram illustrate how CLR is associated with the operating system/hardware along with the class libraries. Here, the runtime is actually CLR. 

Role of CLR in the Execution of a C# Program

  • Suppose we 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.

Benefits of CLR

  • It optimizes runtime execution and program interaction. 
  • The programs can run on any system that supports CLR without needing recompilation.
  • It provides type safety and checks for potential security risks in code.
  • It automatically handles memory allocation and cleanup.
  • It allows code written in different languages to work together.
  • Makes it easier to build scalable, multithreaded applications.

Next Article
Article Tags :

Similar Reads