Duration (Pacific Standard Time):
To (Pacific Standard Time):
  • None
User Action:
  • None

ValueType Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides the base class for value types.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)


Public MustInherit Class ValueType
Name Description
System_CAPS_protmethod ValueType()

Initializes a new instance of the ValueType class.

Name Description
System_CAPS_pubmethod Equals(Object)

Indicates whether this instance and a specified object are equal.(Overrides Object.Equals(Object).)

System_CAPS_protmethod Finalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode()

Returns the hash code for this instance.(Overrides Object.GetHashCode().)

System_CAPS_pubmethod GetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethod ToString()

Returns the fully qualified type name of this instance.(Overrides Object.ToString().)

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.

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and StructureEnd Structure in Visual Basic) to support the creation of value types.

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.

Imports System.Numerics

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) Then
         Throw New ArgumentException("value2 is not a number.")
         ' Use BigInteger as common integral type
         If isInteger(value1) And IsInteger(value2) Then
            Dim bigint1 As BigInteger = CType(value1, BigInteger)
            Dim bigInt2 As BigInteger = CType(value2, BigInteger)
            Return CType(BigInteger.Compare(bigint1, bigint2), NumericRelationship)
         ' At least one value is floating point; use Double.
            Dim dbl1, dbl2 As Double
               dbl1 = CDbl(value1)
            Catch e As OverflowException
               Console.WriteLine("value1 is outside the range of a Double.")
            End Try

               dbl2 = CDbl(value2)
            Catch e As OverflowException
               Console.WriteLine("value2 is outside the range of a Double.")
            End Try
            Return CType(dbl1.CompareTo(dbl2), NumericRelationship)
         End If
      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 _
                 Or TypeOf value Is BigInteger) 
   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
      Return 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 BigInteger OrElse
         TypeOf value Is Decimal OrElse
         TypeOf value Is Double OrElse
         TypeOf value Is Single
   End Function
End Class

The following example illustrates calls to the methods of the Utility class.

Module Example
   Public Sub Main()
      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
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top