2 out of 2 rated this helpful - Rate this topic

ValueType Class

Updated: August 2011

Provides the base class for value types.

System.Object
  System.ValueType
    System.Enum

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class ValueType

The ValueType type exposes the following members.

  Name Description
Protected method Supported by the XNA Framework Supported by Portable Class Library ValueType Initializes a new instance of the ValueType class.
Top
  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library Equals Indicates whether this instance and a specified object are equal. (Overrides Object.Equals(Object).)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Returns the hash code for this instance. (Overrides Object.GetHashCode().)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns the fully qualified type name of this instance. (Overrides Object.ToString().)

In XNA Framework 3.0, this member is inherited from Object.ToString().


In Portable Class Library Portable Class Library, this member is inherited from Object.ToString().
Top

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.

Except for 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.


using System;
using System.Numerics;


public class Utility
{
   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 BigInteger as common integral type
      if (IsInteger(value1) && IsInteger(value2)) {
         BigInteger bigint1 = (BigInteger) value1;
         BigInteger bigint2 = (BigInteger) value2;
         return (NumericRelationship) BigInteger.Compare(bigint1, bigint2);
      }
      // At least one value is floating point; use Double.
      else {
         Double dbl1 = 0;
         Double dbl2 = 0;
         try {
            dbl1 = Convert.ToDouble(value1);
         }
         catch (OverflowException) {
            Console.WriteLine("value1 is outside the range of a Double.");
         }
         try {
            dbl2 = Convert.ToDouble(value2);
         }
         catch (OverflowException) {
            Console.WriteLine("value2 is outside the range of a Double.");
         }
         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 
              || value is BigInteger); 
   }

   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 BigInteger ||
              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.


public class Example
{
   public static void Main()
   {
      Console.WriteLine(Utility.IsNumeric(12));
      Console.WriteLine(Utility.IsNumeric(true));
      Console.WriteLine(Utility.IsNumeric('c'));
      Console.WriteLine(Utility.IsNumeric(new DateTime(2012, 1, 1)));
      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);
   }
}
// The example displays the following output:
//       True
//       False
//       False
//       False
//       False
//       True
//       False
//       True
//       False
//       12.1 GreaterThan 12


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

August 2011

Expanded the Remarks section.

Customer feedback.

October 2010

Added an example.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Error in code example

In the code example it says to using System.Numerics, but I am unable to add that namespace, why?

Importing the System.Numerics Namespace

The compiler is unable to resolve a reference to the System.Numerics namespace because you haven't added a reference to the assembly in which it resides. In order to successfully compile the example, you have to add a reference to System.Numerics.dll. For example, to compile the example from the command line, you'd need to use syntax like

   csc Example.cs /r:system.numerics.dll

for the C# compiler or

   vbc Example.vb /r:system.numerics.dll

for the Visual Basic compiler.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

Casting to ValueType is still boxing

Re: the example showing constraint to value-types, it should be stressed that while ValueType can be used to *restrict* values to value-types, casting to ValueType (implicitly or explicitly) is still a boxing operation; only the concrete known value-types ("DateTime", "int", etc) can be handled directly as value-types - ValueType itself is treated as a class (so boxing).

Another option is to use generics - for example (although note that this doesn't actually use the "value" parameter, except for generic-type-inference):

public static bool IsFloat<T>(T value) where T : struct 
{
switch(Type.GetTypeCode(typeof(T))) {
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
default:
return false;
}
}


Generics can avoid boxing (where possible) by way of the per-value-type JIT for generics, and the inbuilt use of the "constrained" opcode. The latter allows provable methods to be called by either static-call (avoiding a box) or virtual-call (demands a box for value-types), depending on which is most appropriate.

Compiler prevents inheriting new classes from ValueType
In an attempt to create value types that had inheritance, I tried creating a class that inherits from System.ValueType. Doesn't work. The compiler disallows this, returning error:
"error CS0644: 'Test.ByValue' cannot derive from special class 'System.ValueType'"

The only way to create a type that "inherits" from Value type is to use the keyword struct or Enum.
Enum and Structures
Enumerators are derived from System.Enum which in turn is derived from System.ValueType  in spite the fact that ValueTypes derived types are sealed which in turn makes System.Enum sealed. But this is possible because the enumerators does not explicitly inherit from Enum; the inheritance relationship is handled implicitly by the compiler. But structures are directly derived from System.ValueType so they are automatically sealed.