Option Infer 语句

允许在声明变量时使用本地类型推理。

语法

Option Infer { On | Off }

部件

术语 定义
On 可选。 启用本地类型推理。
Off 可选。 禁用本地类型推理。

注解

Option Infer若要在文件、键入Option Infer On文件或文件顶部设置,请在Option Infer Off任何其他源代码之前进行设置。 如果文件中设置 Option Infer 的值与 IDE 或命令行中设置的值冲突,则文件中的值优先。

设置为 /> 时,可以声明局部变量,而无需显式声明数据类型。 编译器从其初始化表达式的类型推断变量的数据类型。

在下图中, Option Infer 已打开。 声明 Dim someVar = 2 中的变量按类型推理声明为整数。

以下屏幕截图显示了启用 Option Infer 时的 IntelliSense:

显示选项推理打开时的 IntelliSense 视图的屏幕截图。

在下图中, Option Infer 已关闭。 声明中 Dim someVar = 2 的变量声明为 Object 按类型推理。 在此示例中, “选项严格 ”设置设置为“ 关闭 ”在 “编译页”项目设计器(Visual Basic)上。

以下屏幕截图显示了关闭 Option Infer 时的 IntelliSense:

显示选项推理关闭时的 IntelliSense 视图的屏幕截图。

注释

当变量声明为变量 Object时,运行时类型可以在程序运行时更改。 Visual Basic 执行称为 装箱取消装箱 的作,以在值 Object 类型之间进行转换,从而使执行速度变慢。 有关装箱和取消装箱的信息,请参阅 Visual Basic 语言规范

类型推理在过程级别应用,并且不适用于类、结构、模块或接口中的过程外部。

有关详细信息,请参阅 本地类型推理

当选项推理语句不存在时

如果源代码不包含Option Infer语句,则使用编译页上的选项推理设置(Visual Basic)。 如果使用命令行编译器,则使用 -optioninfer 编译器选项。

在 IDE 中设置 Option Infer

  1. 在解决方案资源管理器 中,选择一个项目。 在“项目”菜单上,单击“属性” 。

  2. 单击“编译”选项卡。

  3. “选项推理 ”框中设置值。

创建新项目时,“编译”选项卡上的“选项推理”设置将设置为“VB 默认值”对话框中的“选项推断”设置。 若要访问 “VB 默认值 ”对话框,请在 “工具” 菜单上单击“ 选项”。 在 选项 对话框中,展开 项目和解决方案,然后单击 VB 默认值VB 默认值中的初始默认设置为 On

在命令行上设置 Option Infer

vbc 命令中包含 -optioninfer 编译器选项。

默认数据类型和值

下表描述了在语句中 Dim 指定数据类型和初始值设定项的各种组合的结果。

指定的数据类型? 指定的初始值设定项? 示例: 结果
Dim qty 如果 Option Strict 为 off(默认值),则变量设置为 Nothing

如果 Option Strict 已打开,则会发生编译时错误。
是的 Dim qty = 5 如果 Option Infer 处于打开状态(默认值),则变量采用初始值设定项的数据类型。 请参阅 本地类型推理

如果Option Infer已关闭且Option Strict关闭,则变量采用数据类型。Object

如果 Option Infer 已关闭且 Option Strict 处于打开状态,则会发生编译时错误。
是的 Dim qty As Integer 变量初始化为数据类型的默认值。 有关详细信息,请参阅 Dim 语句
是的 是的 Dim qty As Integer = 5 如果初始值设定项的数据类型无法转换为指定的数据类型,则会发生编译时错误。

示例 1

以下示例演示了语句如何 Option Infer 启用本地类型推理。

' 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

示例 2

以下示例演示当变量标识为变量 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

另请参阅