Common Language Runtime

Of fundamental importance to the .NET framework is the fact that programs are executed within a managed execution environment provided by the Common Language Runtime. The CLR greatly improves runtime interactivity between programs, portability, security, development simplicity, cross-language integration, and provides an excellent foundation for a rich set of class libraries.

Absolutely key to these benefits is the way .NET programs are compiled. Each language targeting .NET compiles source code into metadata and Microsoft Intermediate Language (MSIL) code. Metadata includes a complete specification for a program including all its types, apart from the actual implementation of each function. These implementations are stored as MSIL, which is machine-independent code that describes the instructions of a program. The CLR uses this “blueprint” to bring a .NET program to life at runtime, providing services far beyond what is possible with the traditional approach—compiling code directly to assembly language.

Key features of the CLR include the following:

Runtime interactivity

Programs can richly interact with each other at runtime through their metadata. A program can search for new types at runtime, then instantiate and invoke methods on those types.

Portability

Programs can be run without recompiling on any operating system and processor combination that supports the CLR. A key element of this platform independence is the runtime’s JIT ( Just-In-Time Compiler), which compiles the MSIL code it is fed to native code that runs on the underlying platform.

Security

Security considerations permeate the design of the .NET Framework. Key to making this possible is CLR’s ability to analyze MSIL instructions as being safe or unsafe.

Simplified deployment

An assembly is a completely self-describing package that contains all the metadata and MSIL of a program. Deployment can be as easy as copying the assembly to the client computer.

Versioning

An assembly can function properly with new versions of assemblies it depends on without recompilation. Key to making this possible is the ability to resolve all type references though metadata.

Simplified development

The CLR provides many features that greatly simplify development, including services such as garbage collection, exception handling, debugging, and profiling.

Cross language integration

The Common Type System (CTS) of the CLR defines the types that can be expressed in metadata and MSIL and the possible operations that can be performed on those types. The CTS is broad enough to support many different languages, including Microsoft languages, such as C#, VB.NET, and VC.NET, and such third party languages as COBOL, Eiffel, Haskell, Mercury, ML, Oberon, Perl, Python, Smalltalk, and Scheme.

The Common Language Specification (CLS) defines a subset of the CTS, which provides a common standard that enables .NET languages to share and extend each other’s libraries. For instance, an Eiffel programmer can create a class that derives from a C# class and override its virtual methods.

Interoperability with legacy code

The CLR provides interoperation with the vast base of existing software written in COM and C. .NET types can be exposed as COM types, and COM types can be imported as .NET types. In addition, the CLR provides PInvoke, which is a mechanism that enables C functions, structs, and callbacks to be easily used from within in a .NET program.

Get C# Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.