ValueType Sınıf

Tanım

Değer türleri için temel sınıfı sağlar.

public ref class ValueType abstract
public abstract class ValueType
[System.Serializable]
public abstract class ValueType
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ValueType
type ValueType = class
[<System.Serializable>]
type ValueType = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ValueType = class
Public MustInherit Class ValueType
Devralma
ValueType
Türetilmiş
Öznitelikler

Açıklamalar

ValueType sanal yöntemlerini Object , değer türleri için daha uygun uygulamalarla geçersiz kılar. Ayrıca Enumbkz. ' den ValueTypedevralan .

Veri türleri, değer türlerine ve başvuru türlerine ayrılır. Değer türleri yığınla ayrılmış veya bir yapıda satır içinde ayrılmıştır. Başvuru türleri yığın ayrılmıştır. Hem başvuru hem de değer türleri üst temel sınıfından Objecttüretilir. Bir değer türünün nesne gibi davranmasının gerekli olduğu durumlarda, değer türünün yığında başvuru nesnesi gibi görünmesini sağlayan bir sarmalayıcı ayrılır ve değer türünün değeri buna kopyalanır. Sistemin bir değer türü içerdiğini bilmesi için sarmalayıcı işaretlenir. Bu işlem kutulama olarak bilinir ve ters işlem de kutu açma olarak bilinir. Kutulama ve kutu açma, herhangi bir türün nesne olarak ele alınmasına izin verir.

Değer türleri için örtük temel sınıf olsa ValueType da, doğrudan öğesinden ValueType devralan bir sınıf oluşturamazsınız. Bunun yerine, tek tek derleyiciler değer türlerinin oluşturulmasını desteklemek için bir dil anahtar sözcüğü veya yapısı (C# ve StructureVisual Basic'te ...End Structure gibistruct) sağlar.

.NET Framework değer türleri için temel sınıf olarak hizmet vermenin yanı sıra, ValueType yapı genellikle doğrudan kodda kullanılmaz. Ancak, olası bağımsız değişkenleri tüm nesneler yerine değer türleriyle kısıtlamak veya bir yöntemin bir dizi farklı değer türünü işlemesine izin vermek için yöntem çağrılarında parametre olarak kullanılabilir. Aşağıdaki örnek, başvuru türlerinin yöntemlere geçirilmesini nasıl ValueType önlediğini gösterir. adlı Utility dört yöntem içeren bir sınıf tanımlar: IsNumericbağımsız değişkeninin bir sayı olup olmadığını gösterir; IsIntegerbağımsız değişkeninin tamsayı olup olmadığını gösterir; IsFloatbağımsız değişkeninin kayan noktalı sayı Compareolup olmadığını ve iki sayısal değer arasındaki ilişkiyi gösteren öğesini belirtir. Her durumda, yöntem parametreleri türündedir ValueTypeve başvuru türlerinin yöntemlere geçirilmesi engellenir.

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);
   }
}
open System
open System.Numerics

module Utility =
    type NumericRelationship =
        | GreaterThan = 1 
        | EqualTo = 0
        | LessThan = -1
   
    let isInteger (value: ValueType) =
        match value with
        | :? sbyte | :? int16 | :? int32 | :? int64 
        | :? byte | :? uint16  | :? uint32 
        | :? uint64 | :? bigint -> true
        | _ -> false

    let isFloat (value: ValueType) = 
        match value with
        | :? float32 | :? float | :? decimal -> true
        | _ -> false

    let tryToBigInt (value: ValueType) =
        match value with
        | :? sbyte as v -> bigint v |> Some
        | :? int16 as v -> bigint v |> Some
        | :? int32 as v -> bigint v |> Some
        | :? int64 as v -> bigint v |> Some
        | :? byte as v -> bigint v |> Some
        | :? uint16 as v -> bigint v |> Some
        | :? uint32 as v -> bigint v |> Some
        | :? uint64 as v -> bigint v |> Some
        | :? float32 as v -> bigint v |> Some
        | _ -> None

    let isNumeric (value: ValueType) =
        isInteger value || isFloat value

    let compare (value1: ValueType) (value2: ValueType) =
        if isNumeric value1 |> not then
            invalidArg "value1" "value1 is not a number."
        elif isNumeric value2 |> not then
            invalidArg "value2" "value2 is not a number."

        // Use BigInteger as common integral type
        match tryToBigInt value1, tryToBigInt value2 with
        | Some bigint1, Some bigint2 ->
            BigInteger.Compare(bigint1, bigint2) |> enum<NumericRelationship>

        // At least one value is floating point use Double.
        | _ ->
            let dbl1 = 
                try
                    Convert.ToDouble value1
                with
                | :? OverflowException ->
                    printfn "value1 is outside the range of a Double."
                    0.
                | _ -> 0.

            let dbl2 = 
                try
                    Convert.ToDouble value2
                with
                | :? OverflowException ->
                    printfn "value2 is outside the range of a Double."
                    0.
                | _ -> 0.
                
            dbl1.CompareTo dbl2 |> enum<NumericRelationship>
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

Aşağıdaki örnekte sınıfının yöntemlerine yapılan çağrılar gösterilmektedir Utility .

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
printfn $"{Utility.isNumeric 12}"
printfn $"{Utility.isNumeric true}"
printfn $"{Utility.isNumeric 'c'}"
printfn $"{Utility.isNumeric (DateTime(2012, 1, 1))}"
printfn $"{Utility.isInteger 12.2}"
printfn $"{Utility.isInteger 123456789}"
printfn $"{Utility.isFloat true}"
printfn $"{Utility.isFloat 12.2}"
printfn $"{Utility.isFloat 12}"
printfn $"{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

Oluşturucular

ValueType()

ValueType sınıfının yeni bir örneğini başlatır.

Yöntemler

Equals(Object)

Bu örnek ile belirtilen bir nesnenin eşit olup olmadığını gösterir.

GetHashCode()

Bu örneğe ilişkin karma kodu döndürür.

GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Bu örneğin tam tür adını döndürür.

Şunlara uygulanır

Ayrıca bkz.