Option Infer 语句Option Infer Statement

允许声明变量时使用局部类型推理。Enables the use of local type inference in declaring variables.

语法Syntax

Option Infer { On | Off }

部件Parts

术语Term DefinitionDefinition
On 可选。Optional. 启用局部类型推理。Enables local type inference.
Off 可选。Optional. 禁用局部类型推理。Disables local type inference.

备注Remarks

若要在文件中设置 Option Infer,请在任何其他源代码之前、文件顶部键入 Option Infer OnOption Infer OffTo set Option Infer in a file, type Option Infer On or Option Infer Off at the top of the file, before any other source code. 如果为文件中 Option Infer 设置的值与在 IDE 中或在命令行上设置的值冲突,则文件中的值优先。If the value set for Option Infer in a file conflicts with the value set in the IDE or on the command line, the value in the file has precedence.

Option Infer 设置为 On时,你可以无需显式声明数据类型就声明本地变量。When you set Option Infer to On, you can declare local variables without explicitly stating a data type. 编译器会从其初始化表达式的类型推断出变量的数据类型。The compiler infers the data type of a variable from the type of its initialization expression.

在下图中,Option Infer 处于打开状态。In the following illustration, Option Infer is turned on. 声明 Dim someVar = 2 中的变量由类型推理声明为整数。The variable in the declaration Dim someVar = 2 is declared as an integer by type inference.

以下屏幕截图显示了选项推断为启用时的 IntelliSense:The following screenshot shows IntelliSense when Option Infer is on:

当选项推断为 on 时显示 IntelliSense 视图的屏幕截图。

在下图中,Option Infer 处于关闭状态。In the following illustration, Option Infer is turned off. 声明 Dim someVar = 2 中的变量由类型推理声明为 ObjectThe variable in the declaration Dim someVar = 2 is declared as an Object by type inference. 在此示例中,在 "编译" 页的 "项目设计器" (Visual Basic)上," Option Strict " 设置设置为 "关闭"。In this example, the Option Strict setting is set to Off on the Compile Page, Project Designer (Visual Basic).

以下屏幕截图显示了选项推断为 off 时的 IntelliSense:The following screenshot shows IntelliSense when Option Infer is off:

当选项推理为 off 时显示 IntelliSense 视图的屏幕截图。

备注

变量声明为 Object 时,可在程序运行时更改运行时类型。When a variable is declared as an Object, the run-time type can change while the program is running. Visual Basic 执行称为装箱取消装箱的操作,以便在 Object 和值类型之间进行转换,这会使执行速度变慢。Visual Basic performs operations called boxing and unboxing to convert between an Object and a value type, which makes execution slower. 有关装箱和取消装箱的信息,请参阅Visual Basic 语言规范For information about boxing and unboxing, see the Visual Basic Language Specification.

类型推理适用于过程级,且在类、结构、模块或接口中的过程之外不适用。Type inference applies at the procedure level, and does not apply outside a procedure in a class, structure, module, or interface.

有关其他信息,请参阅局部类型推理For additional information, see Local Type Inference.

当 Option Infer 语句不存在时When an Option Infer Statement Is Not Present

如果源代码不包含 Option Infer 语句,则使用 "编译" 页上的 "推断设置"选项,将使用 "项目设计器" (Visual Basic)If the source code does not contain an Option Infer statement, the Option Infer setting on the Compile Page, Project Designer (Visual Basic) is used. 如果使用命令行编译器,则使用-optioninfer编译器选项。If the command-line compiler is used, the -optioninfer compiler option is used.

若要在 IDE 中设置 Option InferTo set Option Infer in the IDE

  1. 在“解决方案资源管理器”中,选择一个项目。In Solution Explorer, select a project. 在“项目”菜单上,单击“属性”。On the Project menu, click Properties.

  2. 单击“编译”选项卡。Click the Compile tab.

  3. 设置 "选项推断框" 中的值。Set the value in the Option infer box.

创建新项目时,"编译" 选项卡上的 "选择推断设置" 设置为 " VB 默认值" 对话框中的 "推断设置"选项When you create a new project, the Option Infer setting on the Compile tab is set to the Option Infer setting in the VB Defaults dialog box. 若要访问 " VB 默认值" 对话框,请在 "工具" 菜单上单击 "选项"。To access the VB Defaults dialog box, on the Tools menu, click Options. 在“选项”对话框中,展开“项目和解决方案”,然后单击“VB 默认值”。In the Options dialog box, expand Projects and Solutions, and then click VB Defaults. 默认情况下, VB默认设置中的初始默认设置为 OnThe initial default setting in VB Defaults is On.

若要设置命令行上的 Option InferTo set Option Infer on the command line

vbc命令中包含-optioninfer编译器选项。Include the -optioninfer compiler option in the vbc command.

默认数据类型和值Default Data Types and Values

下表描述了指定 Dim 语句中数据类型和初始值设定项的各种组合的结果。The following table describes the results of various combinations of specifying the data type and initializer in a Dim statement.

是否指定数据类型?Data type specified? 是否指定初始值设定项?Initializer specified? 示例Example 结果Result
No No Dim qty 如果 Option Strict 处于关闭状态(默认),则将变量设置为 NothingIf Option Strict is off (the default), the variable is set to Nothing.

如果 Option Strict 处于打开状态,则发生编译时错误。If Option Strict is on, a compile-time error occurs.
No Yes Dim qty = 5 如果 Option Infer 处于打开状态(默认),则变量采用初始值设定项的数据类型。If Option Infer is on (the default), the variable takes the data type of the initializer. 请参阅局部类型推理See Local Type Inference.

如果 Option InferOption Strict 均处于关闭状态,则变量采用 Object 的数据类型。If Option Infer is off and Option Strict is off, the variable takes the data type of Object.

如果 Option Infer 处于关闭状态但 Option Strict 处于打开状态,则发生编译时错误。If Option Infer is off and Option Strict is on, a compile-time error occurs.
Yes No Dim qty As Integer 将变量初始化为数据类型的默认值。The variable is initialized to the default value for the data type. 有关详细信息,请参阅Dim 语句For more information, see Dim Statement.
Yes Yes Dim qty As Integer = 5 如果初始值设定项的数据类型不可转换为指定数据类型,则会发生编译时错误。If the data type of the initializer is not convertible to the specified data type, a compile-time error occurs.

示例Example

下例演示了 Option Infer 语句启用局部类型推理的方式。The following examples demonstrate how the Option Infer statement enables local type inference.

' Enable Option Infer before trying these examples.

' Variable num is an Integer.
Dim num = 5

' Variable dbl is a Double.
Dim dbl = 4.113

' Variable str is a String.
Dim str = "abc"

' Variable pList is an array of Process objects.
Dim pList = Process.GetProcesses()

' Variable i is an Integer.
For i = 1 To 10
    Console.WriteLine(i)
Next

' Variable item is a string.
Dim lst As New List(Of String) From {"abc", "def", "ghi"}

For Each item In lst
    Console.WriteLine(item)
Next

' Variable namedCust is an instance of the Customer class.
Dim namedCust = New Customer With {.Name = "Blue Yonder Airlines",
                                   .City = "Snoqualmie"}

' Variable product is an instance of an anonymous type.
Dim product = New With {Key .Name = "paperclips", .Price = 1.29}

' If customers is a collection of Customer objects in the following 
' query, the inferred type of cust is Customer, and the inferred type
' of custs is IEnumerable(Of Customer).
Dim custs = From cust In customers 
            Where cust.City = "Seattle" 
            Select cust.Name, cust.ID

示例Example

下例演示了变量被标识为 Object 时运行时类型可以改变的情况。The following example demonstrates that the run-time type can differ when a variable is identified as an Object.

' Disable Option Infer when trying this example.

Dim someVar = 5
Console.WriteLine(someVar.GetType.ToString)

' If Option Infer is instead enabled, the following
' statement causes a run-time error. This is because
' someVar was implicitly defined as an integer.
someVar = "abc"
Console.WriteLine(someVar.GetType.ToString)

' Output:
'  System.Int32
'  System.String

另请参阅See also