The main ways in which value types are different from reference types
are as follows:
-
A value of a value type is stored in a variable
whereas a value of a reference type is pointed to by some variable.
-
A local variable that is of some value type is stored on the stack
whereas a value of a reference type is stored on the heap.
-
A local variable that is of some value type comes into being when the block
(in which it is declared)
is entered,
and it ceases to exist when the block is exited.
This is quite different from an object
(that is of some class,
and is pointed to by some reference variable):
the object is created by the use of
New
and ceases to exist when the garbage collector says so.
-
If a value is passed as an argument to a method,
it will be copied into the parameter
(unless ByRef is present).
This will be time-consuming if the size of the value is large.
-
Each value type is derived from
System.ValueType
(which is in turn derived from System.Object).
As well as
declaring our own value types
(such as the structure type Point given in the previous
section),
there are hundreds of value types in the FCL.
Some examples of enumeration types (from the FCL) are:
System.DayOfWeek System.IO.FileMode
System.Drawing.Drawing2D.DashStyle System.Drawing.Drawing2D.FillMode
System.Globalization.DateTimeStyles System.Windows.Forms.ButtonBorderStyle
Some examples of structure types (from the FCL) are:
System.DateTime System.TimeSpan
System.Drawing.Color System.Drawing.Point
System.Drawing.Rectangle System.Drawing.Size