24

The Common Language Infrastructure

One of the first items that C# programmers encounter beyond the syntax is the context under which a C# program executes. This chapter discusses the underpinnings of how C# handles memory allocation and de-allocation, type checking, interoperability with other languages, cross-platform execution, and support for programming metadata. In other words, this chapter investigates the Common Language Infrastructure on which C# relies both at compile time and during execution. It covers the execution engine that governs a C# program at runtime and considers how C# fits into a broader set of languages that are governed by the same execution engine. Because of C#’s close ties with this infrastructure, most of the features that come with the infrastructure are made available to C#.

Defining the Common Language Infrastructure

Instead of generating instructions that a processor can interpret directly, the C# compiler generates instructions in an intermediate language, the Common Intermediate Language (CIL). A second compilation step then occurs, generally at execution time, that converts the CIL to machine code that the processor can understand. Conversion to machine code is still not sufficient for code execution, however. It is also necessary for a C# program to execute under the context of an agent. The agent responsible for managing the execution of a C# program is the Virtual Execution System (VES), more casually referred to as the runtime. (Note that the runtime in this context does not refer to a specific time, such as execution time; rather, the runtime—the VES—is an agent responsible for managing the execution of a C# program.) The runtime is responsible for loading and running programs and providing additional services (security, garbage collection, and so on) to the program as it executes.

The specifications for the CIL and the runtime are contained within an international standard known as the Common Language Infrastructure (CLI).1 The CLI is a key specification for understanding the context in which a C# program executes and how it can seamlessly interact with other programs and libraries, even when they are written in other languages. Note that the CLI does not prescribe the implementation for the standard but rather identifies the requirements for how a CLI framework should behave once it conforms to the standard. This provides CLI implementers with the flexibility to innovate where necessary while still providing enough structure that programs created by one CLI implementation can execute on a different implementation and even on a different operating system.

note
Note the similarity between the CIL and CLI acronyms and the names they stand for. Distinguishing between them now will help you avoid confusion later.

Contained within the CLI standard are specifications for the following:

The Virtual Execution System
The Common Intermediate Language
The Common Type System
The Common Language Specification
Metadata
The framework

This chapter broadens your view of C# to include the CLI, which is critical to how C# programs operate and interact with programs and with the operating system.

CLI Implementations

The primary implementations of the CLI today are next generation .NET, which runs on Windows, Linux and Mac OS; the .NET Framework for Windows; and .NET Multi-platform App UI (MAUI), which is intended as a general cross-platform solution including iOS, macOS, and Android applications. Each implementation of the CLI includes a C# compiler and a set of framework class libraries. The version of C# supported by each, as well as the exact set of classes in the libraries, varies considerably, and many implementations are now only of historical interest. Table 24.1 describes these implementations.

Table 24.1: Implementations of the CLI

Compiler

Description

.NET

After .NET Core version 3.1, the next release was .NET 5. Version number 4 was skipped to avoid any confusion with existing .NET Framework versions. It is cross platform running on Microsoft Windows, macOS, and Linux. It supports desktop, service, and cloud applications.

.NET Multi-platform App UI (MAUI)

MAUI was the successor to Xamarin, allowing cross platform native applications to be written in Microsoft Windows, iOS, macOS, and Android. Its tooling allows for a single code base to be used for all applications, enabling a high degree of code reuse.

Blazor

Blazor supports running .NET code within a browser. It offers various hosting models, including Server, WebAssembly, and Hybrid models. See https://learn.microsoft.com/aspnet/core/blazor/hosting-models for more details. WebAssembly is an open standard that allows executing byte code within a web browser. See https://webassembly.org/ for more details.

.NET Core/CoreCLR

The .NET Core project, as the name implies, contains the core functionality common to a new implementation of .NET. It is an open source and platform-portable rewrite of the .NET Framework designed for high-performance applications. The CoreCLR is the implementation of the CLR for this project. It encompasses versions up through .NET Core 3.1 and has been released for Windows, macOS, Linux, FreeBSD, and NetBSD. However, some of the APIs, such as WPF, only work on Windows. See https://github.com/dotnet/coreclr for more details.

Microsoft .NET Framework

This traditional (and first) version of the CLR is for creating applications that run on Windows. It includes support for Windows Presentation Foundation (WPF), Windows Forms, and ASP.NET. It uses the .NET Framework Base Class Library (BCL).

Mono

An open source, cross-platform implementation of the CLI for many UNIX-based operating systems, mobile operating systems such as Android, and game consoles such as PlayStation and Xbox.

Xamarin

A set of development tools and platform-portable .NET libraries as well as an implementation of the CLR that helps developers create applications that run on Microsoft Windows, iOS, macOS, and Android platforms with a very high degree of code reuse. Xamarin uses the Mono BCL. Xamarin reached end of support in May of 2024.

Microsoft Silverlight

A legacy cross-platform implementation of the CLI intended for creating browser-based web client applications. Microsoft stopped developing Silverlight in 2013.

.NET Compact Framework

A legacy trimmed-down implementation of the .NET Framework designed to run on PDAs, phones, and Xbox 360.

The XNA library and tools for developing Xbox 360 applications are based on the Compact Framework 2.0 release; Microsoft stopped development of XNA in 2013.

.NET Micro Framework

Microsoft’s open source implementation of the CLI for devices so resource constrained that they cannot run the compact framework.

DotGNU Portable.NET

This effort to create a cross-platform implementation of the CLI was decommissioned in 2012.

Shared Source CLI (Rotor)

Between 2001 and 2006, Microsoft released shared-source reference implementations of the CLI licensed for noncommercial use.

While the list is extensive considering the work required to implement the CLI, just three frameworks are the most relevant going forward.

Microsoft .NET Framework

The Microsoft .NET Framework was the first .NET CLI implementation (released in February 2000). As such, it is the most mature framework and has the largest API set. It supports building web, console, and Microsoft Windows client applications. The biggest limitation of the .NET Framework is that it runs on Microsoft Windows only—in fact, it is bundled with Microsoft Windows. Numerous sub-frameworks are included with the Microsoft .NET Framework; the most prominent are described here:

.NET Framework Base Class Library (BCL): Provides types representing the built-in CLI data types, which include support for file IO, fundamental collections classes, custom attributes, string manipulation, and more. The BCL provides the definition for each of the C# native types such as int and string.
ASP.NET: Used to build websites and web-based APIs. This framework has formed the foundation of Microsoft-based websites since its release in 2002. With the introduction of .NET Core in 2016, ASP.NET provides operating system portability along with significant performance improvements and an updated API with greater pattern consistency.
Windows Presentation Foundation (WPF): A graphical user interface framework used to build rich UI applications that run on Microsoft Windows. WPF provides not only a set of UI components but also a declarative language, called eXtensible Application Markup Language (XAML), that enables a hierarchical definition of an application’s user interface.

You will frequently hear the Microsoft .NET Framework referred to simply as the .NET Framework. Notice the capital F, which distinguishes it from a generic implementation of the CLI and the term .NET framework.

.NET Core

.NET Core is the cross-platform implementation of the .NET CLI going forward. Furthermore, .NET Core is an open-source rewrite of the .NET Framework, with a focus on high performance and cross-platform compatibility.

.NET Core consists of the .NET Core Runtime (Core CLR), .NET Core framework libraries, and a set of Dotnet command-line tools that can be used to create and build all available application scenarios. The combination of these components is included in the .NET Core SDK. If you’ve followed along with this book’s examples, you’ve already used .NET Core and the Dotnet tools.

The .NET Core API is compatible with existing .NET Framework, Xamarin, and Mono implementations via .NET Standard, which is discussed in further detail later in the chapter.

The current focus of .NET Core is to build high-performant and portable console applications as well as to serve as the .NET Foundation for ASP.NET Core and Windows 10 Universal Windows Platform (UWP) applications. Additional frameworks are emerging from .NET Core as more and more operating systems are supported.

.NET Multi-platform App UI (MAUI)

This cross-platform development tool includes application UI development support for Microsoft Windows, Android, macOS, and iOS. What makes MAUI especially powerful is that a single code base can be used to produce platform-native user interfaces on each of the operating systems supported.

________________________________________

1. Throughout the chapter, CLI refers to Common Language Infrastructure rather than command-line interface as in dotnet CLI.
{{ snackbarMessage }}
;