Common Type System
|Visual C# Tutorials|
|.NET Framework Tutorials|
Common Type System
|© 2006 Wiley Publishing Inc.|
|This tutorial—Common Type System—is from Professional .NET Framework 2.0, by Joe Duffy. Copyright © 2006 Wiley Publishing, Inc. All rights reserved. This article is reproduced by permission. This tutorial has been edited especially for C# Online.NET. Read the book review!|
Common Type System (CTS)
| A type system is a tractable syntactic method for proving the absence of certain program behaviors by
classifying phrases according to the kinds of values they comprise.
|— Benjamin C. Pierce, Types and Programming Languages|
| Ultimately, all programs are built from data types. At the core of every language are built-in data types,
ways of combining them to form new types, and ways of naming the new types so they can be used like the built-in types.
|— Jim Miller, The Common Language Infrastructure Annotated Standard|
The Common Language Runtime (CLR)—or more precisely any implementation of the Common Language Infrastructure (CLI) specification—executes code inside the bounds of a well-defined type system, called the Common Type System (CTS). The CTS is part of the CLI, standardized through the ECMAand International Organization for Standardization (ISO) international standards bodies, with representatives from industry and academia. It defines a set of structures and services that programs targeting the CLR may use, including a rich type system for building abstractions out of built-in and custom abstract data-types. In other words, the CTS constitutes the interface between managed programs and the runtime itself.
In addition to being the interface, the CTS introduces a set of rules and axioms that define verifiable type safety. The process of verification categorizes code as either type-safe or -unsafe, the former categorization of which will guarantee safe execution within the engine. Type-safe execution avoids a set of memory corruption risks, which executing unverifiable programs could lead to. The runtime permits execution of such programs, however, leading to great power and flexibility at the risk of encountering corruption and unexpected failures.
This unified type system governs all access, manipulation, and combination of data in memory. It enables static detection and resolution of certain classes of programming errors, a structured way in which to build and reuse abstractions, assistance to compiler authors through a safe and abstract virtual execution system (VES), and a self-description mechanism for programs using rich metadata. Type safety and metadata are two primary platform features that have provided the largest productivity, security, and reliability benefits the platform has to offer. Other factors include runtime services, such as Garbage Collection, and the wealth of APIs that the Framework offers. Each of these will be discussed extensively in future chapters.
Thinking in terms of "pure CTS" is often difficult. Nearly all programmers work with a concrete language, such as C#, VB, C++/CLI, or Python, when writing managed libraries and applications. Languages provide their own unique view of the runtime system, either abstracting away, hiding, or sometimes even exaggerating certain parts in different ways. But they all compile down to the same fundamental set of constructs. This diversity is one reason why the CLR is such a great programming environment and can readily support an array of unique languages. With that said, it can also be a source of challenges when attempting to understand and/or bridge two languages’ unique view over the same underlying type system. This chapter should help to clarify this.