ValueType Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Fornisce la classe base per i tipi di valore.
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
- Ereditarietà
-
ValueType
- Derivato
- Attributi
Commenti
ValueType esegue l'override dei metodi virtuali Object da con implementazioni più appropriate per i tipi valore. Vedere anche Enum , che eredita da ValueType .
I tipi di dati sono separati in tipi valore e tipi riferimento. I tipi valore sono allocati nello stack o allocati inline in una struttura . I tipi riferimento vengono allocati nell'heap. Sia i tipi riferimento che i tipi valore derivano dalla classe di base finale Object . Nei casi in cui è necessario che un tipo valore si comporti come un oggetto, un wrapper che rende il tipo di valore simile a un oggetto di riferimento viene allocato nell'heap e il valore del tipo di valore viene copiato al suo interno. Il wrapper è contrassegnato in modo che il sistema sappia che contiene un tipo valore. Questo processo è noto come boxing e il processo inverso è noto come unboxing. La boxing e l'unboxing consentono di trattare qualsiasi tipo come un oggetto.
Sebbene ValueType sia la classe di base implicita per i tipi valore, non è possibile creare una classe che eredita direttamente da ValueType . Al contrario, i singoli compilatori forniscono una parola chiave o un costrutto del linguaggio (ad esempio struct
in C# e Structure
...End Structure
in Visual Basic) per supportare la creazione di tipi valore.
Oltre a fungere da classe base per i tipi valore nel .NET Framework, la struttura in genere non viene ValueType usata direttamente nel codice. Tuttavia, può essere usato come parametro nelle chiamate al metodo per limitare i possibili argomenti ai tipi valore anziché a tutti gli oggetti o per consentire a un metodo di gestire diversi tipi di valore. Nell'esempio seguente viene illustrato ValueType come impedire che i tipi riferimento vengano passati ai metodi. Definisce una classe denominata che contiene quattro metodi: , che indica se l'argomento è un numero, , che indica se l'argomento è un numero intero; , che indica se l'argomento è un numero a virgola mobile e , che indica la relazione tra due Utility
IsNumeric
valori IsInteger
IsFloat
Compare
numerici. In ogni caso, i parametri del metodo sono di tipo e non è possibile passare i ValueType tipi riferimento ai metodi.
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
Nell'esempio seguente vengono illustrate le chiamate ai metodi della Utility
classe .
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
Costruttori
ValueType() |
Inizializza una nuova istanza della classe ValueType. |
Metodi
Equals(Object) |
Indica se questa istanza e un oggetto specificato sono uguali. |
GetHashCode() |
Restituisce il codice hash per l'istanza. |
GetType() |
Ottiene l'oggetto Type dell'istanza corrente. (Ereditato da Object) |
MemberwiseClone() |
Crea una copia superficiale dell'oggetto Object corrente. (Ereditato da Object) |
ToString() |
Restituisce il nome completo del tipo di questa istanza. |