可讓您在宣告變數時使用局部類型推斷。
語法
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 中的變數會依類型推斷宣告為整數。
下列螢幕快照顯示 [選項推斷] 開啟時 IntelliSense:
在下圖中, Option Infer 已關閉。 宣告 Dim someVar = 2 中的變數會依類型推斷宣告為 Object 。 在此範例中,[選項嚴格] 設定會設定為 [編譯頁面]、[項目設計工具] [Visual Basic] 上的 [關閉]。
下列螢幕快照顯示 [選項推斷] 關閉時的 IntelliSense:
備註
當變數宣告為 Object時,運行時間類型可以在程式執行時變更。 Visual Basic 會執行稱為 Boxing 和 unboxing 的作業,以在 和 實值型別之間進行 Object 轉換,讓執行速度變慢。 如需 Boxing 和 unboxing 的相關信息,請參閱 Visual Basic 語言規格。
類型推斷會在程式層級套用,而且不適用於類別、結構、模組或介面中的程式外部。
如需詳細資訊,請參閱 本機類型推斷。
當選項推斷語句不存在時
如果原始程式碼不包含 Option Infer 語句,則會使用編譯頁面、項目設計工具 (Visual Basic) 上的 Option Infer 設定。 如果使用命令行編譯程式,則會使用 -optioninfer 編譯程序選項。
在 IDE 中設定 Option Infer
在 [方案總管] 中,選取專案。 按一下 [專案] 功能表上的 [屬性]。
按一下 [編譯] 索引標籤。
在 [ 選項推斷 ] 方塊中設定值。
當您建立新的專案時,[編譯] 索引標籤上的 [選項推斷] 設定會設定為 [VB 預設值] 對話框中的 [選項推斷] 設定。 若要存取 [VB 預設值 ] 對話框,請單擊 [ 工具] 功能表上的 [ 選項]。 在 [ 選項 ] 對話框中,展開 [ 專案和方案],然後按兩下 [VB 預設值]。
VB 預設值中的初始預設設定為 On。
在命令行上設定選項推斷
在 vbc 命令中包含 -optioninfer 編譯程序選項。
默認數據類型和值
下表描述在語句中 Dim 指定數據類型和初始化表達式的各種組合結果。
| 指定的數據類型? | 指定的初始化表達式? | 範例 | 結果 |
|---|---|---|---|
| 否 | 否 | Dim qty |
如果 Option Strict 為 off (預設值),變數會設定為 Nothing。如果 Option Strict 為 開啟,就會發生編譯時期錯誤。 |
| 否 | 是的 | Dim qty = 5 |
如果 Option Infer 為 on (預設值),變數會採用初始化表達式的數據類型。 請參閱 本機類型推斷。如果 Option Infer 為 off 且 Option Strict 為 off,則變數會採用 的 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