Common Type System
   HOME

TheInfoList



OR:

In Microsoft's
.NET Framework The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
, the Common Type System (CTS) is a standard that specifies how type definitions and specific values of types are represented in computer memory. It is intended to allow programs written in different programming languages to easily share information. As used in
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, a type can be described as a definition of a set of values (for example, "all integers between 0 and 10"), and the allowable operations on those values (for example, addition and subtraction). The specification for the CTS is contained in Ecma standard 335, "Common Language Infrastructure (CLI) Partitions I to VI." The
CLI CLI may refer to: Computing * Call Level Interface, an SQL database management API * Command-line interface, of a computer program * Command-line interpreter or command language interpreter; see List of command-line interpreters * CLI (x86 instruc ...
and the CTS were created by Microsoft, and the Microsoft .NET framework is an implementation of the standard.


Functions of the Common Type System

*To establish a framework that helps enable cross-language integration, type safety, and high performance code execution. *To provide an
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
model that supports the complete implementation of many programming languages. *To define rules that languages must follow, which helps ensure that objects written in different languages can interact with each other. *The CTS also defines the rules that ensures that the data types of objects written in various languages are able to interact with each other. *The CTS also specifies the rules for type visibility and access to the members of a type, i.e. the CTS establishes the rules by which assemblies form scope for a type, and the Common Language Runtime enforces the visibility rules. *The CTS defines the rules governing
type inheritance In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
, virtual methods and object lifetime. *Languages supported by .NET can implement all or some common data types… When
rounding Rounding means replacing a number with an approximate value that has a shorter, simpler, or more explicit representation. For example, replacing $ with $, the fraction 312/937 with 1/3, or the expression with . Rounding is often done to obta ...
fractional values, the halfway-to-even ("banker's") method is used by default, throughout the Framework. Since version 2, "Symmetric Arithmetic Rounding" (round halves away from zero) is also available by programmer's option. *it is used to communicate with other languages


Type categories

The common type system supports two general categories of types: ;Value types :
Value type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
s directly contain their data, and instances of value types are either allocated on the
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations. ;Reference types :
Reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
s store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The type of a reference type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates. The following example written in
Visual Basic .NET Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visua ...
shows the difference between reference types and value types: Imports System Class Class1 Public Value As Integer = 0 End Class 'Class1 Class Test Shared Sub Main() Dim val1 As Integer = 0 Dim val2 As Integer = val1 val2 = 123 Dim ref1 As New Class1() Dim ref2 As Class1 = ref1 ref2.Value = 123 Console.WriteLine("Values: , ", val1, val2) Console.WriteLine("Refs: , ", ref1.Value, ref2.Value) End Sub 'Main End Class 'Test The output of the above example Values: 0, 123 Refs: 123, 123


Boxing and unboxing


Boxing

Converting value types to reference types is also known as
boxing Boxing (also known as "Western boxing" or "pugilism") is a combat sport in which two people, usually wearing protective gloves and other protective equipment such as hand wraps and mouthguards, throw punches at each other for a predetermined ...
. As can be seen in the example below, it is not necessary to tell the compiler an Int32 is boxed to an object, because it takes care of this itself. Int32 x = 10; object o = x ; // Implicit boxing Console.WriteLine("The Object o = ",o); // prints out "The Object o = 10" However, an Int32 can always be explicitly boxed like this: Int32 x = 10; object o = (object) x; // Explicit boxing Console.WriteLine("The object o = ",o); // prints out "The object o = 10"


Unboxing

The following example intends to show how to unbox a reference type back to a value type. First an Int32 is boxed to an object, and then it is unboxed again. Note that unboxing requires explicit cast. Int32 x = 5; object o1 = x; // Implicit Boxing x = (int)o1; // Explicit Unboxing


See also

*
.NET Framework The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
* Blittable types *
Common Language Infrastructure The Common Language Infrastructure (CLI) is an open specification and technical standard originally developed by Microsoft and standardized by ISO/IEC (ISO/IEC 23271) and Ecma International (ECMA 335) that describes executable code and a runt ...


References


External links


Microsoft developer's guide describing the CTSBuilt-in types in the .NET Framework
{{Common Language Infrastructure Common Language Infrastructure