Export (0) Print
Expand All

7.1 Value Types and Reference Types

Visual Studio .NET 2003

Although value types and reference types can be similar in terms of declaration syntax and usage, their semantics are distinct.

Reference types are stored on the run-time heap; they may only be accessed through a reference to that storage. Because reference types are always accessed through references, their lifetime is managed by the .NET Framework. Outstanding references to a particular instance are tracked and the instance is destroyed only when no more references remain. A variable of reference type contains a reference to a value of that type, a value of a more derived type, or a null reference. A null reference is a reference that refers to nothing; it is not possible to do anything with a null reference except assign it. Assignment to a variable of a reference type creates a copy of the reference rather than a copy of the referenced value. For a variable of a reference type, the default value is a null reference.

Value types are stored directly on the stack, either within an array or within another type; their storage can only be accessed directly. Because value types are stored directly within variables, their lifetime is determined by the lifetime of the variable that contains them. When the location containing a value type instance is destroyed, the value type instance is also destroyed. Value types are always accessed directly; it is not possible to create a reference to a value type. Prohibiting such a reference makes it impossible to refer to a value class instance that has been destroyed. Because value types are always NotInheritable, a variable of a value type always contains a value of that type. Because of this, the value of a value type cannot be a null reference, nor can it reference an object of a more derived type. Assignment to a variable of a value type creates a copy of the value being assigned. For a variable of a value type, the default value is the result of initializing each variable member of the type to its default value.

The following example shows the difference between reference types and value types:

Imports System

Class Class1
    Public Value As Integer = 0
End Class

Module Test
    Sub Main()
        Dim val1 As Integer = 0
        Dim val2 As Integer = val1
        val2 = 123
        Dim ref1 As Class1 = New Class1()
        Dim ref2 As Class1 = ref1
        ref2.Value = 123
        Console.WriteLine("Values: " & val1 & ", " & val2)
        Console.WriteLine("Refs: " & ref1.Value & ", " & ref2.Value)
    End Sub
End Module

The output of the program is:

Values: 0, 123
Refs: 123, 123

The assignment to the local variable val2 does not impact the local variable val1 because both local variables are of a value type (the type Integer) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123; affects the object that both ref1 and ref2 reference.

Value types and reference types are unified under the type Object, which is the root type of all types. Because Object is neither a reference nor a value type, it can contain values of either kind and has semantics consistent with the kind of type contained within it at run time.

The following example shows how Object can be used with both value types and reference types:

Imports System

Class Class1
    Public Value As Integer = 0
End Class

Structure Struct1
    Public Value As Integer
End Structure

Module Test
    Sub Main()
        Dim val1 As Object = New Struct1()
        Dim val2 As Object = val1

        val2.Value = 123

        Dim ref1 As Object = New Class1()
        Dim ref2 As Object = ref1

        ref2.Value = 123

        Console.WriteLine("Values: " & val1.Value & ", " & val2.Value)
        Console.WriteLine("Refs: " & ref1.Value & ", " & ref2.Value)
    End Sub
End Module

The output of the program is:

Values: 0, 123
Refs: 123, 123

The assignment to the field of the local variable val2 does not impact the field of the local variable val1 because both local variables are of a value type (the type Integer) and each local variable of a value type has its own storage. In contrast, the assignment ref2.Value = 123 affects the object that both ref1 and ref2 references.

See Also

7.3 Primitive Types | 7.4 Enumerations | 7.6 Structures | 7.5 Classes | 7.7 Standard Modules | 7.8 Interfaces | 7.9 Arrays | 7.10 Delegates | Data Type Summary (Visual Basic Language Reference) | Value Types and Reference Types (Visual Basic Language Concepts)

Show:
© 2015 Microsoft