21 February 2014

Common Type System


       The Common Type System defines how types are declared, used, and managed in the common language runtime, and is also an important part of the runtime's support for cross-language integration.    

  The common type system performs the following functions:
  •  Establishes a framework that helps enable cross-language integration, type safety, and high-performance code execution.
  •  Provides an object-oriented model that supports the complete implementation of many programming languages.
  •  Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.
  •  Provides a library that contains the primitive data types (such as Boolean, Byte, Char, Int32, and UInt64) used in application development.
  • 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, virtual methods and object lifetime. 
  • The common type system supports two general categories of types:
  • Value types:
  • Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations.

Reference types:        

         Reference types 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.

Boxing:

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

Example:

   Int32 x = 10;
   Object o = x; // Implicit boxing
   Console.WriteLine ("The Object o = {0}",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


No comments:

Post a Comment