This chapter will introduce the infrastructure on which the C# language is built. By the end of this chapter, it also is intended that the reader will have a clear understanding of what acronyms such as CLI, CLR, VES, JIT, and .NET mean.
The Common Language Infrastructure (CLI)
C# is an object-oriented programming language. It is essentially a standard defining what constitutes valid syntax. On its own, C# is actually of little use because it depends on the Common Language Infrastructure (CLI) for the compilation and execution of applications. The CLI, in turn, is actually a standard that defines specifications for the following components:
- Virtual Execution System (VES)
- Common Intermediate Language (CIL)
- Common Type System (CTS)
- Common Language Specification (CLS)
In the remainder of this chapter, we will look at each CLI component to build a picture of how the CLI environment fits together.
Common Intermediate Language (CIL)
Unlike the C and C++ compilers which compile the source code down to the machine code understood by the target microprocessor, the C# compiler compiles to an intermediate byte code format known as the Common Intermediate Language (CIL). This code can, in theory, be taken to any system where there is a CLI-compliant Virtual Execution System (VES) and executed. Therefore, there is no need to compile an application for every target platform.
The word “Common” in Common Intermediate Language is used because this format is common to more than just the C# programming language. In fact, any programming language may target the CIL allowing libraries and code modules from different languages to execute together in the same application. Typical languages for which CIL compilation is available include Visual Basic, COBOL, PowerShell, Python, and C++.
Virtual Execution System (VES)
The VES (usually referred to as the runtime) is the environment in which the CIL byte code is executed. The VES reads the byte code generated by the C# compiler and uses something called a Just in Time (JIT) compiler to compile the byte code down to the native machine code of the processor on which it is running. While this code executes, it does so in conjunction with a runtime agent that manages the execution process. As a result, this executing code is known as managed code, and the process handles issues such as garbage collection (to handle memory allocation and de-allocation), memory access, and type safety to ensure that the code does not do anything it is not supposed to do.
A term that is often used in connection with the VES is the Common Language Runtime (CLR). The CLR is officially the name given to Microsoft’s implementation of the VES component of the CLI specification.
It is worth noting that the JIT process can introduce a startup delay in the execution of an application. One option available with .Net to avoid this problem is pre-compiling CLI byte code to native machine code using the NGEN compiler. Because the NGEN compilation must take place on the target processor architecture, this step is often performed at the point that the application in question is installed by the user.
Common Type System (CTS) & Common Language Specification (CLS)
As mentioned previously, several different programming languages target the CLI, allowing, for example, code from C# sources to interact with code from Visual Basic. Each language must have the same concept of how data types are stored in memory to achieve this feat. The CTS, therefore, defines how a CLI-compatible language must view the bit patterns of values and the layout and behavior of objects to ensure interoperability.
The CLS is essentially a subset of the CTS aimed at creating interoperable libraries.
The Framework (Base Class and Framework Class Libraries)
The CLI specifies a set of base classes that must be available to execute CLI code, otherwise known as the Base Class Library (BCL). The BCL contains APIs that enable executing CIL code to interact with the runtime environment and the underlying operating system.
Beyond the basics, there is also the Framework Class Library. This is a Microsoft library that contains APIs for creating graphical user interfaces, database applications, web access, and much more.
Implementations of the CLI
Microsoft’s implementation of the CLI stack is called .NET. Originally only available on Windows systems, Microsoft repackaged .Net in 2014 as an open-source, cross-platform development platform available on Windows, Linux, and macOS.
Another implementation Microsoft provides is Shared Source Common Language Infrastructure (SSCLI). SSCLI, however, is primarily a learning tool and, as such, is licensed under terms that prohibit use as the basis of commercial applications.
The Mono project is an alternative and highly regarded open-source implementation of the CLI stack. This project is sponsored by Microsoft and also makes the C# infrastructure available for Windows, Linux, and macOS platforms.
Finally, if you would like to develop apps for iOS and Android using C#, there is also the Xamarin open-source implementation.