Jump to content

Common Type System

From Wikipedia, the free encyclopedia

In Microsoft's.NET Framework,theCommon Type System(CTS) is a standard that specifies howtypedefinitions 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 inprogramming languages,atypecan 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 inEcmastandard 335, "Common Language Infrastructure (CLI) Partitions I to VI." TheCLIand the CTS were created by Microsoft, and theMicrosoft.NET frameworkis an implementation of the standard.

Functions of the Common Type System[edit]

  • To establish a framework that helps enable cross-language integration, type safety, and high performance code execution.
  • To provide anobject-orientedmodel 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 governingtype inheritance,virtual methods and object lifetime.
  • Languages supported by.NET can implement all or some common data types…

Whenroundingfractional values, thehalfway-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.[1]

  • it is used to communicate with other languages

Type categories[edit]

The common type system supports two general categories of types:

Value types
Value typesdirectly contain their data, and instances of value types are either allocated on thestackor allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations.
Reference types
Reference typesstore a reference to the value's memory address, and are allocated on theheap.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 inVisual Basic.NETshows the difference between reference types and value types:

ImportsSystem

ClassClass1
PublicValueAsInteger=0
EndClass'Class1

ClassTest
SharedSubMain()
Dimval1AsInteger=0
Dimval2AsInteger=val1
val2=123
Dimref1AsNewClass1()
Dimref2AsClass1=ref1
ref2.Value=123
Console.WriteLine("Values: {0}, {1}",val1,val2)
Console.WriteLine("Refs: {0}, {1}",ref1.Value,ref2.Value)
EndSub'Main
EndClass'Test

The output of the above example

Values: 0, 123
Refs: 123, 123

Bo xing and unbo xing[edit]

Bo xing[edit]

Converting value types to reference types is also known asbo xing.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.

Int32x=10;
objecto=x;// Implicit bo xing
Console.WriteLine("The Object o = {0}",o);// prints out "The Object o = 10"

However, an Int32 can always be explicitly boxed like this:

Int32x=10;
objecto=(object)x;// Explicit bo xing
Console.WriteLine("The object o = {0}",o);// prints out "The object o = 10"

Unbo xing[edit]

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 unbo xing requires explicit cast.

Int32x=5;
objecto1=x;// Implicit Bo xing
x=(int)o1;// Explicit Unbo xing

See also[edit]

References[edit]

  1. ^"MidpointRounding Enumeration".Microsoft Docs.

External links[edit]