.NET Documentation

Core Concepts: Runtime

.NET Runtime Core Concepts

The .NET runtime is the foundation upon which all .NET applications are built. It manages the execution of your code, provides essential services, and ensures a consistent and robust development experience across various platforms.

Common Language Runtime (CLR)

The CLR is the execution engine of the .NET Framework. It provides the managed environment that controls the execution of .NET programs. Key responsibilities include:

When you compile a .NET application, the source code is first compiled into an Intermediate Language (IL). The CLR then takes this IL code and compiles it into native machine code that the specific processor can understand.

// Example of IL code execution
            // The CLR manages this process behind the scenes.
            public void SayHello()
            {
                System.Console.WriteLine("Hello, .NET Runtime!");
            }

Garbage Collection (GC)

The GC is a crucial part of the CLR's memory management. It automatically reclaims memory that is no longer being used by the application, preventing memory leaks and simplifying development. The GC works in generations, prioritizing the cleanup of recently allocated objects.

Understanding the GC's behavior can help in optimizing application performance, especially for applications with high memory usage.

Intermediate Language (IL) and Just-In-Time (JIT) Compilation

IL, also known as Common Intermediate Language (CIL), is a platform-agnostic, CPU-independent language. All .NET code is compiled into IL first. The JIT compiler, part of the CLR, translates this IL into native machine code specific to the processor architecture and operating system at runtime.

JIT Compilation Process

1. Compilation to IL: Source code (.cs, .vb) is compiled into .dll or .exe files containing IL code.

2. Loading by CLR: The CLR loads the IL code.

3. JIT Compilation: As methods are called, the JIT compiler translates their IL code into native machine code.

4. Execution: The native code is executed by the processor.

Managed Code vs. Unmanaged Code

Managed code is code that is executed under the control of the CLR. It benefits from services like automatic memory management, type safety, and exception handling. Most .NET code is managed.

Unmanaged code is code that runs outside the CLR, such as C++ code or operating system APIs. While powerful, it requires manual memory management and doesn't inherently benefit from the CLR's services. .NET provides mechanisms like Platform Invoke (P/Invoke) and COM Interop to allow managed code to interact with unmanaged code.

Assemblies and Modules

An assembly is the fundamental unit of deployment, versioning, and security for .NET applications. It's a collection of types and resources that are visible to or included by the CLR. Assemblies are typically found in .dll or .exe files.

An assembly can contain one or more modules. A module is a single file (often a .dll or .exe) that contains the IL code and metadata for a part of an assembly.

Metadata

Metadata is data about the data. In .NET, metadata describes the types, members, and other constructs defined in an assembly. This information is crucial for the CLR to understand and execute the code. Metadata includes:

The CLR uses metadata for tasks like type checking, reflection, and security verification.