ValueType ValueType ValueType ValueType Class

定義

提供實值型別 (Value Type) 的基底類別。Provides the base class for value types.

public ref class ValueType abstract
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class ValueType
type ValueType = class
Public MustInherit Class ValueType
繼承
ValueTypeValueTypeValueTypeValueType
衍生
屬性

備註

ValueType 覆寫虛擬方法,從Object較適合實作實值型別。ValueType overrides the virtual methods from Object with more appropriate implementations for value types. 另請參閱Enum,該項則繼承自ValueTypeSee 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. 參考和實值類型衍生自的 ultimate 基底類別ObjectBoth 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. 此程序稱為 boxing,以及反向的程序稱為 unboxing。This process is known as boxing, and the reverse process is known as unboxing. Boxing 和 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. 相反地,個別的編譯器會提供語言關鍵字,或建構 (例如struct在 C# 和Structure...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. 它會定義名為類別Utility,其中包含四種方法: IsNumeric,這表示其引數是否是一個數字,IsInteger,,指出其引數是否為整數的類型,IsFloat,這表示其引數是否是浮點數; 和Compare,表示兩個數值之間的關聯性。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.")
      Else
         ' 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.
         Else   
            Dim dbl1, dbl2 As Double
            Try
               dbl1 = CDbl(value1)
            Catch e As OverflowException
               Console.WriteLine("value1 is outside the range of a Double.")
            End Try
               
            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(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
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

建構函式

ValueType() ValueType() ValueType() ValueType()

初始化 ValueType 類別的新執行個體。Initializes a new instance of the ValueType class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指示這個執行個體和指定的物件是否相等。Indicates whether this instance and a specified object are equal.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回此執行個體的完整類型名稱。Returns the fully qualified type name of this instance.

適用於

另請參閱