ValueType Class
Updated: October 2010
Provides the base class for value types.
Assembly: mscorlib (in mscorlib.dll)
ValueType overrides the virtual methods from Object with more appropriate implementations for value types. See also Enum, which inherits from ValueType.
Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.
Aside from serving as the base class for value types in the .NET Framework, the ValueType structure is generally not used directly in code. However, it can be used as a parameter in method calls to restrict possible arguments to value types instead of all objects, or to permit a method to handle a number of different value types. The following example illustrates how ValueType prevents reference types from being passed to methods. It defines a class named Utility that contains four methods: IsNumeric, which indicates whether its argument is a number; IsInteger, which indicates whether its argument is an integer; IsFloat, which indicates whether its argument is a floating-point number; and Compare, which indicates the relationship between two numeric values. In each case, the method parameters are of type ValueType, and reference types are prevented from being passed to the methods.
Public Class Utility Public Enum NumericRelationship As Integer GreaterThan = 1 EqualTo = 0 LessThan = -1 End Enum Public Shared Function Compare(value1 As ValueType, value2 As ValueType) _ As NumericRelationship If Not IsNumeric(value1) Then Throw New ArgumentException("value1 is not a number.") Else If Not IsNumeric(value2) Throw New ArgumentException("value1 is not a number.") End If ' Use Int64 or UInt64 as common integral type If IsInteger(value1) AndAlso IsInteger(value2) Then Dim useUnsigned As Boolean If (TypeOf value1 Is ULong AndAlso CULng(value1) > Int64.MaxValue) OrElse _ (TypeOf value2 Is ULong AndAlso CULng(value2) > Int64.MaxValue) Then useUnsigned = True End If If useUnsigned Then If Math.Sign(Convert.ToDouble(value1)) < 0 Then Return NumericRelationship.LessThan End If If Math.Sign(Convert.ToDouble(value2)) < 0 Then Return NumericRelationship.GreaterThan End If Return CType(CULng(value1).CompareTo(CULng(value2)), NumericRelationship) Else Dim long1 As Long = CLng(value1) Dim long2 As Long = CLng(value2) Return CType(long1.CompareTo(long2), NumericRelationship) End If ' At least one value is floating point; use Double. Else Dim dbl1 As Double = 0 Dim dbl2 As Double = 0 dbl1 = Convert.ToDouble(value1) dbl2 = Convert.ToDouble(value2) Return CType(dbl1.CompareTo(dbl2), NumericRelationship) End If End Function Public Shared Function IsInteger(value As ValueType) As Boolean Return (TypeOf value Is SByte Or TypeOf value Is Int16 Or TypeOf value Is Int32 _ Or TypeOf value Is Int64 Or TypeOf value Is Byte Or TypeOf value Is UInt16 _ Or TypeOf value Is UInt32 Or TypeOf value Is UInt64) End Function Public Shared Function IsFloat(value As ValueType) As Boolean Return (TypeOf value Is Single Or TypeOf value Is Double Or TypeOf value Is Decimal) End Function Public Shared Function IsNumeric(value As ValueType) As Boolean If Not (typeof value Is Byte OrElse _ typeof value Is Int16 OrElse _ typeof value Is Int32 OrElse _ TypeOf value Is Int64 OrElse _ TypeOf value Is SByte OrElse _ TypeOf value Is UInt16 OrElse _ TypeOf value Is UInt32 OrElse _ TypeOf value Is UInt64 OrElse _ TypeOf value Is Decimal OrElse _ TypeOf value Is Double OrElse _ TypeOf value Is Single) Then Return False Else Return True End If End Function End Class
The following example illustrates calls to the methods of the Utility class.
Module Example Public Sub Main() Console.WriteLine(Utility.IsNumeric(12)) Console.WriteLine(Utility.IsNumeric(True)) Console.WriteLine(Utility.IsNumeric("c"c)) Console.WriteLine(Utility.IsNumeric(#01/01/2012#)) Console.WriteLine(Utility.IsInteger(12.2)) Console.WriteLine(Utility.IsInteger(123456789)) Console.WriteLine(Utility.IsFloat(True)) Console.WriteLine(Utility.IsFloat(12.2)) Console.WriteLine(Utility.IsFloat(12)) Console.WriteLine("{0} {1} {2}", 12.1, Utility.Compare(12.1, 12), 12) End Sub End Module ' The example displays the following output: ' True ' False ' False ' False ' False ' True ' False ' True ' False ' 12.1 GreaterThan 12
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.