ValueType Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Provides the base class for value types.

Inheritance Hierarchy

System.Object
  System.ValueType
    System.Enum

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public MustInherit Class ValueType
[ComVisibleAttribute(true)]
public abstract class ValueType

The ValueType type exposes the following members.

Constructors

  Name Description
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ValueType Initializes a new instance of the ValueType class.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals Indicates whether this instance and a specified object are equal. (Overrides Object.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Returns the hash code for this instance. (Overrides Object.GetHashCode().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns the fully qualified type name of this instance. (Overrides Object.ToString().)

Top

Remarks

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 Structure…End 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.

Public Class Utility
   Public Shared outputBlock As System.Windows.Controls.TextBlock 

   Public Enum NumericRelationship As Integer
      GreaterThan = 1
      EqualTo = 0
      LessThan = -1
   End Enum

   Public Shared Function Compare(ByVal value1 As ValueType, ByVal value2 As ValueType) _
                                  As NumericRelationship
      If Not IsNumeric(value1) Then
         Throw New ArgumentException("value1 is not a number.")
      ElseIf Not IsNumeric(value2) Then
         Throw New ArgumentException("value2 is not a number.")
      Else
         ' 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, dbl2 As Double
            Try
               dbl1 = CDbl(value1)
            Catch e As OverflowException
               outputBlock.Text &= "value1 is outside the range of a Double." & vbCrLf
            End Try

            Try
               dbl2 = CDbl(value2)
            Catch e As OverflowException
               outputBlock.Text &= "value2 is outside the range of a Double." & vbCrLf
            End Try
            Return CType(dbl1.CompareTo(dbl2), NumericRelationship)
         End If
      End If
   End Function

   Public Shared Function IsInteger(ByVal 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(ByVal 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(ByVal 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
using System;

public class Utility
{
   public static System.Windows.Controls.TextBlock outputBlock;

   public enum NumericRelationship
   {
      GreaterThan = 1,
      EqualTo = 0,
      LessThan = -1
   };

   public static NumericRelationship Compare(ValueType value1, ValueType value2)
   {
      if (!IsNumeric(value1))
         throw new ArgumentException("value1 is not a number.");
      else if (!IsNumeric(value2))
         throw new ArgumentException("value1 is not a number.");

     // Use Int64 or UInt64 as common integral type
      if (IsInteger(value1) && IsInteger(value2)) {
         bool useUnsigned = false;
         if ((value1 is ulong && ((ulong)value1 > Int64.MaxValue)) || 
             (value2 is ulong && ((ulong) value2 > Int64.MaxValue)))
            useUnsigned = true;

         if (useUnsigned) {
            if (Math.Sign(Convert.ToDouble(value1)) < 0) 
               return NumericRelationship.LessThan;
            if (Math.Sign(Convert.ToDouble(value2)) < 0) 
               return NumericRelationship.GreaterThan;   
            return (NumericRelationship) (((ulong) value1).CompareTo((ulong) value2));            
         }
         else {    
            long long1 = (long) value1;
            long long2 = (long) value2;
            return (NumericRelationship) long1.CompareTo(long2);
         }
      }
      // At least one value is floating point; use Double.
      else
      {
         Double dbl1 = 0;
         Double dbl2 = 0;
         try
         {
            dbl1 = Convert.ToDouble(value1);
         }
         catch (OverflowException)
         {
            outputBlock.Text += "value1 is outside the range of a Double." + "\n";
         }
         try
         {
            dbl2 = Convert.ToDouble(value2);
         }
         catch (OverflowException)
         {
            outputBlock.Text += "value2 is outside the range of a Double." + "\n";
         }
         return (NumericRelationship)dbl1.CompareTo(dbl2);
      }
   }

   public static bool IsInteger(ValueType value)
   {
      return (value is SByte || value is Int16 || value is Int32
              || value is Int64 || value is Byte || value is UInt16
              || value is UInt32 || value is UInt64);
   }

   public static bool IsFloat(ValueType value)
   {
      return (value is float | value is double | value is Decimal);
   }

   public static bool IsNumeric(ValueType value)
   {
      if (!(value is Byte ||
              value is Int16 ||
              value is Int32 ||
              value is Int64 ||
              value is SByte ||
              value is UInt16 ||
              value is UInt32 ||
              value is UInt64 ||
              value is Decimal ||
              value is Double ||
              value is Single))
         return false;
      else
         return true;
   }
}

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

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Utility.outputBlock = outputBlock

      outputBlock.Text &= Utility.IsNumeric(12) & vbCrLf
      outputBlock.Text &= Utility.IsNumeric(True) & vbCrLf
      outputBlock.Text &= Utility.IsNumeric("c"c) & vbCrLf
      outputBlock.Text &= Utility.IsNumeric(#1/1/2012#) & vbCrLf
      outputBlock.Text &= Utility.IsInteger(12.2) & vbCrLf
      outputBlock.Text &= Utility.IsInteger(123456789) & vbCrLf
      outputBlock.Text &= Utility.IsFloat(True) & vbCrLf
      outputBlock.Text &= Utility.IsFloat(12.2) & vbCrLf
      outputBlock.Text &= Utility.IsFloat(12) & vbCrLf
      outputBlock.Text += String.Format("{0} {1} {2}", 12.1, Utility.Compare(12.1, 12), 12) & vbCrLf
   End Sub
End Module
' The example displays the following output:
'       True
'       False
'       False
'       False
'       False
'       True
'       False
'       True
'       False
'       12.1 GreaterThan 12
public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Utility.outputBlock = outputBlock;

      outputBlock.Text += Utility.IsNumeric(12) + "\n";
      outputBlock.Text += Utility.IsNumeric(true) + "\n";
      outputBlock.Text += Utility.IsNumeric('c') + "\n";
      outputBlock.Text += Utility.IsNumeric(new DateTime(2012, 1, 1)) + "\n";
      outputBlock.Text += Utility.IsInteger(12.2) + "\n";
      outputBlock.Text += Utility.IsInteger(123456789) + "\n";
      outputBlock.Text += Utility.IsFloat(true) + "\n";
      outputBlock.Text += Utility.IsFloat(12.2) + "\n";
      outputBlock.Text += Utility.IsFloat(12) + "\n";
      outputBlock.Text += String.Format("{0} {1} {2}", 12.1, Utility.Compare(12.1, 12), 12) + "\n";
   }
}
// The example displays the following output:
//       True
//       False
//       False
//       False
//       False
//       True
//       False
//       True
//       False
//       12.1 GreaterThan 12

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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

See Also

Reference

Change History

Date

History

Reason

October 2010

Added an example.

Customer feedback.