Option Infer 语句
允许声明变量时使用局部类型推理。
语法
Option Infer { On | Off }
组成部分
术语 | 定义 |
---|---|
On |
可选。 启用局部类型推理。 |
Off |
可选。 禁用局部类型推理。 |
注解
若要在文件中设置 Option Infer
,请在任何其他源代码之前、文件顶部键入 Option Infer On
或 Option Infer Off
。 如果为文件中 Option Infer
设置的值与在 IDE 中或在命令行上设置的值冲突,则文件中的值优先。
将 Option Infer
设置为 On
时,你可以无需显式声明数据类型就声明本地变量。 编译器会从其初始化表达式的类型推断出变量的数据类型。
在下图中,Option Infer
处于打开状态。 声明 Dim someVar = 2
中的变量由类型推理声明为整数。
以下屏幕截图显示了 Option Infer 开启时的 IntelliSense:
在下图中,Option Infer
处于关闭状态。 声明 Dim someVar = 2
中的变量由类型推理声明为 Object
。 在此示例中,项目设计器 (Visual Basic) 的“编译”页上的 Option Strict 设置设置为“关”。
以下屏幕截图显示了 Option Infer 关闭时的 IntelliSense:
注意
变量声明为 Object
时,可在程序运行时更改运行时类型。 Visual Basic 执行称为装箱和取消装箱的操作以在 Object
和值类型间转换,这减缓了执行速度。 有关装箱和取消装箱的信息,请参阅 Visual Basic 语言规范。
类型推理适用于过程级,且在类、结构、模块或接口中的过程之外不适用。
有关其他信息,请参阅本地类型推断。
当 Option Infer 语句不存在时
如果源代码不包含 Option Infer
语句,则使用 项目设计器(Visual Basic)的编译页 上的 Option Infer 设置。 如果使用命令行编译器,请使用 -optioninfer 编译器选项。
若要在 IDE 中设置 Option Infer
在“解决方案资源管理器”中,选择一个项目。 在“项目”菜单上,单击“属性” 。
单击“编译” 选项卡。
设置“Option infer”框中的值。
创建新项目时,将“编译”选项卡上的“Option Infer”设置为“VB 默认值”对话框中的“Option Infer”设置。 若要访问“工具”菜单上的“VB 默认值”对话框,请单击“选项”。 在“选项”对话框中,展开“项目和解决方案”,然后单击“VB 默认值”。 “VB 默认值”中的初始默认设置为 On
。
若要设置命令行上的 Option Infer
在 vbc 命令中包含 -optioninfer 编译器选项。
默认数据类型和值
下表描述了指定 Dim
语句中数据类型和初始值设定项的各种组合的结果。
是否指定数据类型? | 是否指定初始值设定项? | 示例 | 结果 |
---|---|---|---|
否 | 否 | Dim qty |
如果 Option Strict 处于关闭状态(默认),则将变量设置为 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