Common Type System—Type Unification


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Type Unification

All types in the CTS have a common base type at the root of their type hierarchy: System.Object. As we’ll see throughout this chapter, this unification provides a lot of flexibility in how we can pass instances of types around inside the type system. It also means that every type inherits a common group of members, such as methods to convert instances into their text representation, compare instances with each other for equality, and so on. The result is that any instance of any type can be treated as "just an object" to implement some general-purpose functionality. This turns out to be extremely convenient.

The type hierarchy in the CTS is split into two primary trees: reference types and value types. Reference types derive from System.Object directly, while value types derive instead from the special CTS type System.ValueType (which itself derives from System.Object). A diagram of the hierarchy of type abstractions and some specific built-in types is shown in Figure 2-1. It contains a number of special constructs that we will also take a look at throughout this chapter, such as interfaces and enumerations, each of which has a special status in the type system.


Figure 2-1: CTS type hierarchy.


Notice that a number of primitive data types are listed under the value type hierarchy. Most of the very fundamental types that you take for granted live here. You’ll find the following:

  • System.Boolean, or bool in textual IL, is a type whose values can take on one of two values: true or false; in the IL these are represented as 1 and 0, respectively. The size of its storage is actually a full byte (8 bits), not 1 bit as you might imagine, to align on native memory boundaries and make operations on them more efficient.
  • System.Char, or just char in textual IL, representing a single unsigned double-byte (16-bit) Unicode character; this includes, for example, "a", "5" "Æ", and "á", among many, many others.
  • System.SByte, Int16, Int32, Int64, or int8, int16, int32, and int16 in textual IL, each representing a signed integer of 1, 2, 4, and 8 bytes (8, 16, 32, and 64 bits), respectively. Signed simply indicates values may be in the negative or positive range.
  • System.Byte, UInt16, UInt32, UInt64, or unsigned int8, unsigned int16, unsigned int32, and unsigned int64 in textual IL, each representing an unsigned integer of 1, 2, 4, and 8 bytes (8, 16, 32, and 64 bits), respectively. Unsigned, of course, means that they do not utilize a bit to represent sign and thus cannot represent negative values. It also means that they can use this extra bit to represent twice the number of positive values of their signed counterparts.
  • System.Single, Double, or float32 and float64 in textual IL, represent standard floating point numbers of 4 and 8 bytes (32 bits and 64 bits), respectively. These are used to represent numbers with a whole and fractional part.
  • System.IntPtr, UIntPtr, or native int and unsigned native int in textual IL, are used to represent machine-sized integers, signed and unsigned, respectively. Most often they are used to contain pointers to memory. On 32-bit systems they will contain 4 bytes (32 bits), while on 64-bit systems they will contain 8 bytes (64 bits), for example.
  • System.Void (or just void) is a special data type used to represent the absence of a type. It’s used only in typing signatures for type members, not for storage locations.

From these types can be constructed other forms abstractions in the type hierarchy, for example:

  • Arrays are typed sequences of elements (e.g., System.Int32[]). Arrays are discussed in detail in Chapter 6.
  • Unmanaged and managed pointers to typed storage locations (e.g., System.Byte* and System.Byte&).
  • More sophisticated data structures, both in the reference and value type hierarchy (e.g., struct Pair { int x; int y }).

Chapter 5 describes in further detail more information about each of the primitive types, explains precisely what methods they define, and covers types such as Object, String, and DateTime, which were not mentioned in detail above. We cover enumerations, interfaces, and delegates next.


Previous_Page_.gif Next_Page_.gif

Share this page
  • del.icio.us
  • Facebook
  • Google+
  • StumbleUpon