#38 – Data Type Hierarchy

All types in .NET inherit from another type, except for System.Object (object), which is at the top of the class hierarchy.

Each type is one of:

  • A built-in type
    • Part of the Common Type System (CTS)
    • Available in C# using a keyword for the type, e.g. int, float, string, array, object
  • A custom type
    • Defined in the .NET Framework class library
  • A user-defined type
    • You define in your code

Here is the class hierarchy for all types.  Indentation indicates that a class derives from class listed above it.  Parentheses indicate C# keywords for built-in types.

  • System.Object  (object)
    • System.ValueType
      • System.Enum
        • user-defined enum types
      • System.Boolean  (bool)
      • System.Byte  (byte)
      • System.SByte  (sbyte)
      • System.Int16  (short)
      • System.UInt16  (ushort)
      • System.Int32  (int)
      • System.UInt32  (uint)
      • System.Int64  (long)
      • System.UInt64  (ulong)
      • System.Single  (float)
      • System.Double  (double)
      • System.Decimal  (decimal)
      • System.Char  (char)
      • System.Void  (void)
      • System.DateTime
      • System.Guid
      • System.TimeSpan
      • System.IntPtr
      • System.UIntPtr
      • user-defined struct types
    • System.String  (string)
    • System.Array  (array)
    • System.Type
    • System.Delegate
      • System.MulticastDelegate  (delegate)
    • custom types in .NET Framework
    • user-defined types
    • boxed value types
Advertisements

#32 – Built-In Types

The C# language includes a variety of built-in types that you can use in your C# program.  Each has a built-in C# keyword for the type, but maps to a type in the .NET Framework.  The C# keyword is just shorthand for the complete type name as it exists in the framework.

Here are all of the C# built-in types that are value types, along with their class name in the .NET Framework.

  • bool – System.Boolean – 1 byte  (true|false)
  • byte – System.Byte – 1 byte   (0 to 255)
  • sbyte – System.SByte – 1 byte  (-128 to 127)
  • short – System.Int16 – 2 bytes  (-32,768 to 32,767)
  • ushort – System.UInt16 – 2 bytes  (0 to 65,535)
  • int – System.Int32 – 4 bytes  (-2,147,483,648 to 2,147,483,647)
  • uint – System.UInt32 – 4 bytes  (0 to 4,294,967,295)
  • long – System.Int64 – 8 bytes  (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
  • ulong – System.UInt64 – 8 bytes  (0 to 18,446,744,073,709,551,615)
  • float – System.Single – 4 bytes  (±1.5e−45 to ±3.4e38, 7 digit precision)
  • double – System.Double – 8 bytes  (±5.0e−324 to ±1.7e308, 15-16 digit precision)
  • decimal – System.Decimal – 16 bytes  (±1.0 × 10−28 to ±7.9 × 1028, 28-29 digit precision)
  • char – System.Char – 2 bytes  (U+0000 to U+ffff, UTF16 Unicode character)