Value Types and Reference TypesValue Types and Reference Types
Esistono due tipi di tipi in Visual Basic: tipi di riferimento e tipi di valore.There are two kinds of types in Visual Basic: reference types and value types. Le variabili dei tipi di riferimento archiviano i riferimenti ai relativi dati (oggetti), mentre le variabili dei tipi di valore contengono direttamente i dati.Variables of reference types store references to their data (objects), while variables of value types directly contain their data. Con i tipi di riferimento, due variabili possono fare riferimento allo stesso oggetto. Di conseguenza le operazioni su una variabile possono influire sull'oggetto a cui fa riferimento l'altra variabile.With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. Con i tipi valore, ogni variabile ha una propria copia dei dati e non è possibile che le operazioni su una variabile influiscano sull'altra (tranne nel caso del modificatore ByRef nei parametri).With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of the ByRef modifier on parameters).
Tipi valoreValue Types
Un tipo di dati è un tipo di valore se include i dati all'interno della propria allocazione di memoria.A data type is a value type if it holds the data within its own memory allocation. I tipi di valore includono i seguenti:Value types include the following:
Tutti i tipi di dati numericiAll numeric data types
Boolean
,Char
eDate
Boolean
,Char
, andDate
Tutte le strutture, anche se i relativi membri sono tipi di riferimentoAll structures, even if their members are reference types
Enumerazioni, poiché il tipo sottostante è sempre,,,,,,
SByte
Short
Integer
Long
Byte
UShort
UInteger
oULong
Enumerations, since their underlying type is alwaysSByte
,Short
,Integer
,Long
,Byte
,UShort
,UInteger
, orULong
Ogni struttura è un tipo valore, anche se contiene membri di tipo riferimento.Every structure is a value type, even if it contains reference type members. Per questo motivo, i tipi di valore come Char
e Integer
vengono implementati da strutture di .NET Framework.For this reason, value types such as Char
and Integer
are implemented by .NET Framework structures.
È possibile dichiarare un tipo valore utilizzando la parola chiave riservata, ad esempio Decimal
.You can declare a value type by using the reserved keyword, for example, Decimal
. È anche possibile usare la New
parola chiave per inizializzare un tipo di valore.You can also use the New
keyword to initialize a value type. Questa operazione è particolarmente utile se il tipo dispone di un costruttore che accetta parametri.This is especially useful if the type has a constructor that takes parameters. Un esempio è il Decimal(Int32, Int32, Int32, Boolean, Byte) costruttore, che compila un nuovo Decimal
valore dalle parti fornite.An example of this is the Decimal(Int32, Int32, Int32, Boolean, Byte) constructor, which builds a new Decimal
value from the supplied parts.
Tipi riferimentoReference Types
Un tipo riferimento archivia un riferimento ai relativi dati.A reference type stores a reference to its data. I tipi di riferimento includono gli elementi seguenti:Reference types include the following:
String
Tutte le matrici, anche se i relativi elementi sono tipi di valoreAll arrays, even if their elements are value types
DelegatiDelegates
Una classe è un tipo di riferimento.A class is a reference type. Si noti che ogni matrice è un tipo di riferimento, anche se i relativi membri sono tipi valore.Note that every array is a reference type, even if its members are value types.
Poiché ogni tipo di riferimento rappresenta una classe .NET Framework sottostante, è necessario utilizzare la parola chiave new operator quando viene inizializzata.Since every reference type represents an underlying .NET Framework class, you must use the New Operator keyword when you initialize it. L'istruzione seguente Inizializza una matrice.The following statement initializes an array.
Dim totals() As Single = New Single(8) {}
Elementi che non sono tipiElements That Are Not Types
Gli elementi di programmazione seguenti non sono qualificati come tipi, perché non è possibile specificarli come tipo di dati per un elemento dichiarato:The following programming elements do not qualify as types, because you cannot specify any of them as a data type for a declared element:
Spazi dei nomiNamespaces
ModuliModules
EventsEvents
Proprietà e procedureProperties and procedures
Variabili, costanti e campiVariables, constants, and fields
Utilizzo del tipo di dati ObjectWorking with the Object Data Type
È possibile assegnare un tipo di riferimento o un tipo di valore a una variabile del Object
tipo di dati.You can assign either a reference type or a value type to a variable of the Object
data type. Una Object
variabile include sempre un riferimento ai dati, non i dati stessi.An Object
variable always holds a reference to the data, never the data itself. Tuttavia, se si assegna un tipo valore a una Object
variabile, si comporta come se contiene i propri dati.However, if you assign a value type to an Object
variable, it behaves as if it holds its own data. Per ulteriori informazioni, vedere tipo di dati Object.For more information, see Object Data Type.
È possibile verificare se una Object
variabile funge da tipo di riferimento o un tipo di valore passandola al IsReference metodo nella Information classe dello Microsoft.VisualBasic spazio dei nomi.You can find out whether an Object
variable is acting as a reference type or a value type by passing it to the IsReference method in the Information class of the Microsoft.VisualBasic namespace. Information.IsReference restituisce True
se il contenuto della Object
variabile rappresenta un tipo di riferimento.Information.IsReference returns True
if the content of the Object
variable represents a reference type.