The Common Language Runtime (CLR) is the execution environment of the .NET Framework. It manages the execution of programs written in languages such as C#, VB.NET, and F# and provides essential runtime services.
- Converts Intermediate Language (IL) into native machine code using the Just-in-Time (JIT) compiler.
- Provides automatic memory management, garbage collection, and exception handling.
- Supports type safety and interoperability between different .NET languages.
Working of CLR
When a C# program runs, its source code is compiled into Intermediate Language (IL). The CLR then manages its execution through the following steps:
- Compilation: The C# compiler converts source code into Intermediate Language (IL) and stores it in an assembly.
- Code Loading: The CLR loads the required assembly and uses its metadata to identify types and other program information.
- JIT Compilation: The JIT compiler converts IL into native machine code during execution.
- Execution: The processor executes the native code while the CLR provides runtime services.
- Memory Management: The garbage collector automatically reclaims memory occupied by unreachable managed objects.
The following diagram illustrates how a C# program is compiled and executed through the CLR.

Key Components of CLR
The CLR contains several components and services that support managed code execution.
1. Just-in-Time (JIT) Compiler
The JIT Compiler converts Intermediate Language (IL) into native machine code during program execution.
- Generates machine code for the target processor.
- Enables execution of IL compiled from supported .NET languages.
2. Garbage Collector (GC)
The Garbage Collector automatically manages memory used by managed objects.
- Allocates memory for managed objects.
- Reclaims memory occupied by objects that are no longer reachable.
3. Common Type System (CTS)
The Common Type System (CTS) defines how data types are declared, used, and managed by the CLR. It helps different .NET languages work with a consistent type system.
CTS classifies types into two main categories:
- Value Types: Store their data directly. Examples include int, float, bool, and struct.
- Reference Types: Store references to objects. Examples include class, string, and arrays.
Note: Value types are not always stored on the stack. Their storage location depends on their usage, such as whether they are local variables or fields of other objects.
4. Common Language Specification (CLS)
The Common Language Specification (CLS) defines a set of rules that .NET languages can follow to achieve language interoperability.
- Provides common rules for publicly exposed types and members.
- Helps code written in different .NET languages interact with one another.
5. Exception Handling
The CLR supports structured exception handling to manage errors during program execution.
- Provides a common exception-handling mechanism across .NET languages.
- Supports constructs such as try, catch, and finally in C#.
Managed and Unmanaged Code in CLR
The CLR primarily executes managed code, but .NET applications can also interact with unmanaged code.
Managed Code
Managed code is code that executes under the control of the CLR. It benefits from runtime services such as garbage collection, exception handling, and type safety.
- C# programs typically compile into IL and execute as managed code.
- The CLR manages memory and other runtime operations.
Unmanaged Code
Unmanaged code executes outside the CLR's managed execution environment. Examples include native C++ applications, Win32 APIs, and native COM components.
- Memory and resource management may require explicit handling.
- Managed applications can interact with unmanaged code through interoperability mechanisms such as Platform Invocation Services (P/Invoke).
Example: Program to demonstrate how a C# application uses managed objects whose memory is handled by the CLR.
using System;
class Geeks{
static void Main(){
// Create a managed object
string message = "Hello, CLR!";
// Display the message
Console.WriteLine(message);
}
}
Output
Hello, CLR!
Explanation:
- The C# compiler converts the program into IL.
- The CLR executes the program using JIT-compiled machine code.
- The CLR manages the string object, including its memory allocation and eventual garbage collection when it becomes unreachable.
Benefits of CLR
- Provides automatic memory management and garbage collection.
- Supports type safety and structured exception handling.
- Enables interoperability between supported .NET languages.
- Reduces the need for manual management of managed object memory.
- Supports multithreading and provides runtime services for application execution.
CLR Versions in .NET Framework
Different versions of .NET Framework use specific versions of the CLR. The following table shows their relationship.
CLR Version | .NET Framework Version |
|---|---|
1.0 | 1.0 |
1.1 | 1.1 |
2.0 | 2.0 |
2.0 | 3.0 |
2.0 | 3.5 |
4.0 | 4.0 |
4.0 | 4.5, 4.5.1, 4.5.2 |
4.0 | 4.6, 4.6.1, 4.6.2 |
4.0 | 4.7, 4.7.1, 4.7.2 |
4.0 | 4.8, 4.8.1 |
Note: Modern .NET, including .NET 6, 8, 9, and 10, uses CoreCLR rather than the .NET Framework CLR versions listed above.