Types in the .NET Framework – Part 1: Value Types

 ^Exam 70-536 TopicsPart 2: Reference Types>

These are my reading notes and practice code examples. The notes are from MCTS Self-Paced Training Kit (Exam 70-536): Microsoft® .NET Framework 2.0—Application Development Foundation, with additions from other sources as noted.

Chapter 1 Framework Fundamentals, Lesson 1 Value Types

  • Value types are types that contain a value rather than a reference to a value stored somewhere in memory. Value types are stored on the stack.
  • There are three categories of value types: built-in types, user-defined types, and enumerations.
  • They are derived from System.Object, so you can call methods like .ToString on value types.

VB Example of using the .ToString method:

Imports System.Console 
Module Module1                       

     Sub Main() 
          Dim iBBValue As Integer 
          iBBValue = 2007                      

          'This line will cause a build error since iBBValue isn't a string type 
          'WriteLine("The year is: " + iBBValue)                      

          WriteLine("The year is: " + iBBValue.ToString)                      

          WriteLine("Press any key to exit") 

     End Sub                      

End Module

Built-in Value Types

  • There are more than 300 built-in value types.
  • The most efficient numeric types for the CLR to compile are 32 bit integer types and doubles.
  • Some of the common  built in value types supplied by the in the .NET framework are:
    (additional source: .NET Framework Class Library Overview)

    Category Type name Data Format VB Type name
    Integer Byte 8 bit unsigned Byte
      SByte 8 bit signed (not CLS compliant) SByte
      Int16 16 bit signed Short
      Int32 32 bit signed Integer
      Int64 64 bit signed Long
      UInt16 16 bit unsigned UShort
      UInt32 32 bit unsigned UInteger
      UInt64 64 bit unsigned ULong
    Floating Point Single 32 bit  floating point (single precision) Single
      Double 64 bit floating point (double precision) Double
    Logical Boolean 32 bit Boolean value (true or false) Boolean
    Other Char 16 bit Unicode character Char
      Decimal 128 bit Decimal
      IntPtr Pointer to a memory address. (Signed Integer with size dependant on the OS) IntPtr (Not a built in type?)
      UintPtr   UintPtr (Not a built-in type?)
      DateTime 64 bit Date
    Class Objects Object Root of the object hierarchy Object
      String Fixed length string of Unicode characters String
  • Value types can be declared nullable. This means that you can distinguish between a variable that has not been assigned a value and one with a zero value.VB Example of declaring a nullable value type variable:
    Imports System.Console 
    Module Module1                       
        Sub Main() 
            Dim iBBValue As Nullable(Of Integer)= Nothing                       
            Write("iBBValue = ") 
            'The following line will cause a run time error 
            If Not iBBValue.HasValue Then 
                WriteLine("This variable has no value") 
                Write("This variable has the value: ") 
            End If                       
            iBBValue = 2007 
            If Not iBBValue.HasValue Then 
                WriteLine("This variable has no value") 
                Write("This variable has the value: ") 
            End If                       
            Write("Press any key to exit") 
        End Sub                       
    End Module

    User Defined Types (Structures)

    • Structures are very similar to classes, except they are value types, so they are created on the stack. Classes are reference types and are created on the heap.
    • Structures have implicitly defined parameterless constructors, so you can only define a constructor that takes parameters. Here’s a VB example:
        Structure BBColor 
              Dim Red As Short 
              Dim Green As Short 
              Dim Blue As Short              
              'You can't override the implicit paramterless  constructor. 
              'This code will cause a design time error 
              'Sub New() 
              '    Red = 255 
              '    Green = 255 
              '    Blue = 255 
              'End Sub              
              'You can provide a constructor with parameters 
              Sub New(ByVal R As Short, ByVal G As Short, ByVal B As Short) 
                  Red = R 
                  Green = G 
                  Blue = B 
              End Sub              
          End Structure              
          Sub Main() 
              'Both of these declarations are valid and use the implicit contstructor 
              Dim SomeColor As New BBColor 
              Dim AnotherColor As BBColor              
              'This declaration uses the user defined constructor 
              Dim YetAnotherColor As New BBColor(255, 0, 0)            
              'Now do something with your BBColor objects... 
          End Sub

    Structures are more efficient to process than classes. You should consider creating a structure if your new type:

    • Represents a single “logical” value
      (although it may have multiple value fields like a point or a vector)
    • Has an instance size less than 16 bytes
    • Will not be changed after creation
    • Will not be cast to a reference type

    VB Example of declaring and using a structure:

    Imports System.Console 
    Module Module1                            
        Structure Car 
            Public Make As String 
            Public Model As String 
            Public Year As Integer                            
            Public Sub New(ByVal _Make As String, ByVal _Model As String, ByVal _Year As Integer) 
                Make = _Make 
                Model = _Model 
                Year = _Year 
            End Sub                            
            Public Overloads Overrides Function ToString() As String 
                Return Year.ToString + " " + Make + " " + Model 
            End Function                            
        End Structure                            
        Sub Main()                            
            Dim BBCar As New Car("Honda", "Civic", 1998) 
            Write("Hit any key to exit") 
        End Sub                            
    End Module


    • Enumerations are a set of symbols with fixed values.
    • The symbols are usually related in some way.
    • The value of enumeration members may optionally be initialized.
    • Enumerations can optionally be declared as one of the following types: Byte, Integer, Long, SByte, Short, UInteger, ULong, or UShort.

    VB Example of using enumerations:

       Enum CoffeeType 
        End Enum      
        Enum WeekDay 
            Sunday = 1 
            Monday = 2 
            Tuesday = 3 
            Wednesday = 4 
            Thursday = 5 
            Friday = 6 
            Saturday = 7 
        End Enum      
        Enum MaxAge As Short 
            infants = 1 
            toddlers = 3 
            children = 12 
            teenagers = 18 
        End Enum      
        Sub Main() 
            Dim FavoriteCoffee As CoffeeType 
            FavoriteCoffee = CoffeeType.Americano 
            'Output: Americano     
            Dim WorkDay As WeekDay = 2 
            System.Console.Write("I work on ") 
            'Output: I work on Monday     
            Dim DayOff As WeekDay = WeekDay.Sunday 
            System.Console.Write("I'm off on ") 
            'Output: I'm off on Sunday     
            Dim OurKidsAge As MaxAge = MaxAge.Teenagers 
            System.Console.Write("Our kids are all ", OurKidsAge.ToString) 
            'Output: Our kids are all teenagers 
            System.Console.Write("Our oldest kid is ", OurKidsAge) 
            'Output: Our oldest kid is 18     
        End Sub

    That’s it for this lesson. I’m looking forward to your questions and comments!

  • 1 Comment Add your own

    • 1. FOF  |  November 1, 2008 at 2:51 pm

      what do you mean “Has an instance size less than 16 bytes
      ” , inthe strucure


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    Trackback this post  |  Subscribe to the comments via RSS Feed

    %d bloggers like this: