The Data Type Class Hierarchy

It is very interesting to note that even the primitive .NET data types are arranged in a "class hierarchy." If you are new to the world of inheritance, you will discover the full details in Chapter 6. Until then, just understand that types at the top of a class hierarchy provide some default behaviors that are granted to the derived types. The relationship between these core system types can be depicted as shown in Figure 3-7.

Figure 3-7. The class hierarchy of System types

Notice that each of these types ultimately derives from System.Object, which defines a set of methods (ToString(), Equals(), GetHashCode(), and so forth) common to all types in the .NET base class libraries (these methods are fully detailed in Chapter 6).

Also note that numerical data types derive from a class named System.ValueType. Descendants of ValueType are automatically allocated on the stack and therefore have a very predictable lifetime and are quite efficient. On the other hand, types that do not have System.ValueType in their inheritance chain (such as System.Type, System.String, System.Array, System.Exception, and System. Delegate) are not allocated on the stack, but on the garbage-collected heap.

Without getting too hung up on the details of System.Object and System.ValueType for the time being (again, more details in Chapter 12), simply know that because a VB 2008 keyword (such as Integer) is shorthand notation for the corresponding system type (in this case, System.Int32), the following is perfectly legal syntax, given that System.Int32 (the VB 2008 Integer) eventually derives from System.Object, and therefore can invoke any of its public members:

Sub Main()

' A VB 2008 Integer is really a shorthand for System.Int32, ' which inherits the following members from System.Object. Console.WriteLine(12.GetHashCode()) ' Prints the type's hash code value. Console.WriteLine(12.Equals(23)) ' Prints False Console.WriteLine(12.ToString()) ' Returns the string value "12"

Console.WriteLine(12.GetType()) ' Prints System.Int32

End Sub

Was this article helpful?

0 0

Post a comment