The Common Language Infrastructure (CLI) is the foundation, or rather a subset, on which .NET Framework is built on. It is the part that Microsoft chose to standardize (ISO/IEC 23271 and ECMA-335) upon the the first release of the Framework and it involves many of the core aspects of the runtime engine and its base class library. Today there are a couple of other implementations of the standard. Mono is the most popular of them and brings most of the functionality of the .NET Framework to Linux and Mac OS X. Another is the DotGNU project.
CLI provides guidelines for developing platforms on which programming language can be built and easily communicate with each other.
The CLI standard defines the following parts:
- Common Type System (CTS)
Defines a common set of rules for data types that are shared by all CTS-languages - Metadata
Information about the structure of the programs. The information is language-agnostic and makes it easier to share information between different programming languages. - Common Language Specification (CLS)
The rules that any CLI-compatible language must conform to be able to fully operate with other languages on the platform. - Virtual Execution System (VES)
Defines the common execution engine that executes the programs built for the CLI. It combines separately generated pieces of code at runtime by using the metadata.
In one sentence: The standard simply defines a virtual execution system, or virtual machine, and the basic components around it, as the base class library.
The goal of is to make it easier for programmer to write platform agnostic programs that can be executed in a safe mode in a virtual execution environment. Another goal is to make it easier for programming languages to interoperate by sharing one common underlying platform with common rules which they all conform to.
Types and Memory management
The Common Type System (CTS) defines value types and reference types. Typical value types are the primitive types, like int and char. These are native to the runtime (the VES) and are stored in a memory location called the stack. String is not a primitive type nor a value type, but a reference type, an instance (object) of a class. A reference type is stored on the heap. These two are named after the way they are handled.
Values are actual values and objects are instances of classes that can be accessed through a reference. Both are bound to a scope and there are no globals.
The stack and the heap are handled by the runtime. The memory occupied by values and objects are allocated and freed by it. In traditional unmanaged languages like C/C++ the programmer needs to handle the memory management by himself. That is a painful process that takes a lot of time. It is most importantly dangerous to handle memory the wrong way. A simple error may cause the computer to crash or worse. Now with memory management, the programmer can focus on productivity instead.
In the .NET Framework there is a garbage collector which discards the references and frees the memory to the heap when it is no longer reachable in code.
There is also functionality as well as a framework for handling exceptions. The runtime shall abort when an exception is thrown and the programmer shall handle it.
Object Oriented Programming
The CLI is developed for native support of Object Oriented Programming (OOP). Every instance of a type can be regarded as an object. There is no difference in the way value types and reference types are accessed, except the way the runtime treats them.
You can use OOP all down to the CIL. There are instructions in the instruction set that can create instances and access the members.
Value types such as the primitive types have wrapper classes that extends from the System.ValueType class.
A static runtime
The Common Language Infrastructure defines a type system that is statically typed and a set of instructions that handles them. The reason was that Microsoft firstly wanted to embrace type safety with static typing, and of course, the statically typed languages was the most popular ones when the .NET project was started. Whereas, it is possible to run dynamically typed languages directly on top of the platform.
However, Microsoft has created the Dynamic Language Runtime (DLR) to make it easier for dynamically typed languages to run on top of it. The DLR is a static library that translates dynamic calls into static actions at runtime using Reflection and expression trees. The DLR can be freely downloaded and redistributed.
Metadata and Reflection
Metadata makes the CIL powerful. It is information about the structure of the program and it is very important during the execution process. The metadata can be edited and even generated at runtime through reflection. You are able to generate a class, method or even IL at runtime.
Common Intermediate Language
An executable program file is called an assembly. It contains the metadata and code written in the so called Common Intermediate Language (CIL). The CIL, or simply IL, has an instruction set with instructions much like the ones of a CPU. There is also an assembly language which is the form of code that is closest to the IL but still readable for humans.
This is the classic Hello World-program handwritten in CIL:
.assembly extern mscorlib {}
.assembly hello {}
.method static public void main() cil managed
{
.entrypoint
.maxstack 1
ldstr "Hello, World!"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
Article about this code: http://en.csharp-online.net/HelloWorld_in_MSIL
Note: It can be assembled with the ILASM utility in .NET Framework and Mono Framework. Likewise you can disassemble an assembly into IL assembler code using ILDASM, or similar.
If you are interested in learning some more about IL you should check this article at CodeProject.
Base Class Library
There is also a rich Base Class Library (BCL) to be implemented. It shall contain the basic functionality that is needed when developing an application for the a CLI-based platform.
Furthermore, the base class library is updated in every build of the .NET Framework.
Note that not all of the BCL-classes in the official .NET implementation are standardized. The APIs of the standardized BCL are listed here.
Compile once. Run everywhere.
The programs written for the CLI are platform agnostic and can be compiled once and later executed on every platform that implements the specification. But Microsoft does only provide one working implementation (.NET Framework) for Windows, and that is a superset of the standard. So if you do not have the specified libraries you will not be able to run your program. Mono gives support for Linux and Mac OS X through their open source implementation of the CLI, including the extensions of the .NET Framework.
This is similar to Java which also has an implementation for every platforms which enables the programs targeting their platform to run. It is not standardized though.
License
Added: July 10, 2009
Microsoft is going to apply the Community Promise license to the ECMA 334 and ECMA 335 specs (CLI and C#). This will make it possible for anyone to implement specifications and do what they want with the product without the permission from Microsoft.
The license is irrevocable once it is applied. This is good for the open source community.
Read more: http://port25.technet.com/archive/2009/07/06/the-ecma-c-and-cli-standards.aspx
Source of information: From Microsoft: C# and CLI under the Community Promise at Miguel de Icaza’s WebLog.
Implementations
Here is a list of the most common implementations of the CLI.
- .NET Framework is Microsoft’s commercial implementation which is built on their execution engine, Common Language Runtime (CLR). Available for Windows, and was formerly (v. 2.0) for Mac OS X too. The CoreCLR is used by Silverlight on Windows and Mac OS X.
- Shared Source Common Language Infrastructure (SSCLI), formerly known as ROTOR, is a reference implementation of the CLR. Distributed under Microsoft Shared Source License.
- .NET Compact Framework is Microsoft’s implementation for portable devices and Xbox 360.
- Mono Framework is an open source implementation of the CLI for Linux, Mac OS X and Windows. The project aims to be almost 100% compatible with .NET Framework.
- Portable.NET is part of the dotGNU project, open source/free software by Free Software Foundation.
External links
Some valuable resources.






Recent Comments