ValueType クラス


値の型の基本クラスを提供します。Provides the base class for value types.

public ref class ValueType abstract
public abstract class ValueType
type ValueType = class
Public MustInherit Class ValueType


ValueType は、Object の仮想メソッドを、値型のより適切な実装でオーバーライドします。ValueType overrides the virtual methods from Object with more appropriate implementations for value types. ValueTypeから継承する Enumも参照してください。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. 参照と値の両方の型は、Objectの最終的な基本クラスから派生します。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.

ValueType は値型の暗黙的な基底クラスですが、ValueType から直接継承するクラスを作成することはできません。Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. 代わりに、個々のコンパイラは、言語のキーワードまたはコンストラクト (やC# Structure... の struct など) を提供End StructureInstead, individual compilers provide a language keyword or construct (such as struct in C# and StructureEnd Structure Visual Basic) で、値型の作成をサポートします。in Visual Basic) to support the creation of value types.

.NET Framework の値型の基底クラスとして機能するのではなく、通常、ValueType 構造はコードで直接使用されません。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. 次の例は、ValueType が参照型がメソッドに渡されないようにする方法を示しています。The following example illustrates how ValueType prevents reference types from being passed to methods. これは、引数が数値であるかどうかを示す4つの IsNumericメソッドを含む Utility という名前のクラスを定義します。IsInteger。引数が整数であるかどうかを示します。IsFloat。引数が浮動小数点数であるかどうかを示します。と Compare。2つの数値の間のリレーションシップを示します。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. どちらの場合も、メソッドパラメーターの型は ValueTypeであり、参照型はメソッドに渡されません。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("value2 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)
      return (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);
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

次の例は、Utility クラスのメソッドの呼び出しを示しています。The following example illustrates calls to the methods of the Utility class.

public class Example
   public static void Main()
      Console.WriteLine(Utility.IsNumeric(new DateTime(2012, 1, 1)));
      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
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



ValueType クラスの新しいインスタンスを初期化します。Initializes a new instance of the ValueType class.



このインスタンスと指定したオブジェクトが等しいかどうかを示します。Indicates whether this instance and a specified object are equal.


このインスタンスのハッシュ コードを返します。Returns the hash code for this instance.


現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(継承元 Object)

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(継承元 Object)

このインスタンスの完全修飾型名を返します。Returns the fully qualified type name of this instance.