Value Types and Reference TypesValue Types and Reference Types

Visual Basic 中有两种类型:引用类型和值类型。There are two kinds of types in Visual Basic: reference types and value types. 引用类型的变量存储对其数据(对象)的引用,而值类型的变量直接包含其数据。Variables of reference types store references to their data (objects), while variables of value types directly contain their data. 对于引用类型,两种变量可引用同一对象;因此,对一个变量执行的操作会影响另一个变量所引用的对象。With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. 对于值类型,每个变量都有自己的数据副本,对一个变量执行的操作不可能影响另一个变量(在参数上传递修饰符的情况除外)。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).

值类型Value Types

如果数据类型在其自身的内存分配中包含数据,则该数据类型为值类型A data type is a value type if it holds the data within its own memory allocation. 值类型包括:Value types include the following:

  • 所有数值数据类型All numeric data types

  • BooleanCharDateBoolean, Char, and Date

  • 所有结构(即使它们的成员是引用类型)All structures, even if their members are reference types

  • 枚举,因为它们的基础类型始终为、、、 SByte Short Integer LongByte 、、 UShort UIntegerULongEnumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong

每个结构都是值类型,即使它包含引用类型成员。Every structure is a value type, even if it contains reference type members. 出于此原因,值类型(如 Char 和) Integer 是由 .NET Framework 结构实现的。For this reason, value types such as Char and Integer are implemented by .NET Framework structures.

您可以使用保留的关键字(例如)声明值类型 DecimalYou can declare a value type by using the reserved keyword, for example, Decimal. 还可以使用 New 关键字初始化值类型。You can also use the New keyword to initialize a value type. 如果类型具有采用参数的构造函数,则此方法特别有用。This is especially useful if the type has a constructor that takes parameters. 这是构造函数的一个示例 Decimal(Int32, Int32, Int32, Boolean, Byte) ,它 Decimal 从提供的部分生成一个新值。An example of this is the Decimal(Int32, Int32, Int32, Boolean, Byte) constructor, which builds a new Decimal value from the supplied parts.

引用类型Reference Types

引用类型存储对其数据的引用。A reference type stores a reference to its data. 引用类型包括:Reference types include the following:

  • String

  • 所有数组(即使它们的元素为值类型)All arrays, even if their elements are value types

  • 类类型,如FormClass types, such as Form

  • 委托Delegates

类是引用类型A class is a reference type. 请注意,每个数组都是引用类型,即使其成员是值类型也是如此。Note that every array is a reference type, even if its members are value types.

由于每个引用类型都表示一个基础 .NET Framework 类,因此必须在初始化时使用New 运算符关键字。Since every reference type represents an underlying .NET Framework class, you must use the New Operator keyword when you initialize it. 下面的语句初始化一个数组。The following statement initializes an array.

Dim totals() As Single = New Single(8) {}  

不是类型的元素Elements That Are Not Types

下面的编程元素不限定为类型,因为您不能将任何这些元素指定为已声明元素的数据类型:The following programming elements do not qualify as types, because you cannot specify any of them as a data type for a declared element:

  • 命名空间Namespaces

  • 模块Modules

  • 事件Events

  • 属性和过程Properties and procedures

  • 变量、常量和字段Variables, constants, and fields

使用 Object 数据类型Working with the Object Data Type

可以将引用类型或值类型分配给 Object 数据类型的变量。You can assign either a reference type or a value type to a variable of the Object data type. Object变量始终保存对数据的引用,而不是数据本身。An Object variable always holds a reference to the data, never the data itself. 但是,如果将值类型分配给 Object 变量,则其行为就像它包含自己的数据一样。However, if you assign a value type to an Object variable, it behaves as if it holds its own data. 有关详细信息,请参阅Object Data TypeFor more information, see Object Data Type.

可以 Object 通过将变量传递给 IsReference Information 命名空间类中的方法来确定变量是作为引用类型还是值类型 Microsoft.VisualBasicYou 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.IsReferenceTrue如果该变量的内容 Object 表示一个引用类型,则返回。Information.IsReference returns True if the content of the Object variable represents a reference type.

另请参阅See also