Option Infer 陳述式

可讓您在宣告變數時使用區域類型推斷。

語法

Option Infer { On | Off }

組件

詞彙 定義
On 選擇性。 啟用區域類型推斷。
Off 選擇性。 停用區域類型推斷。

備註

若要在檔案中設定 Option Infer,請在檔案頂端的任何其他原始程式碼之前輸入 Option Infer OnOption Infer Off。 如果在檔案中設定給 Option Infer 的值與 IDE 或命令列中設定的值衝突,檔案中的值具有優先權。

當您將 Option Infer 設定為 On 時,可以宣告區域變數而不用明確陳述資料類型。 編譯器會從其初始化運算式的類型推斷變數的資料類型。

在下圖中,Option Infer 已開啟。 宣告 Dim someVar = 2 中的變數依類型推斷宣告為整數。

下列螢幕擷取畫面顯示 Option Infer 為 on 時的 IntelliSense:

Screenshot showing IntelliSense view when Option Infer is on.

在下圖中,Option Infer 已關閉。 宣告 Dim someVar = 2 中的變數依類型推斷宣告為 Object。 在此範例中,編譯頁面、專案設計工具 (Visual Basic) 上的 Option Strict 設定會設為 Off

下列螢幕擷取畫面顯示 Option Infer 為 Off 時的 IntelliSense:

Screenshot showing IntelliSense view when Option Infer is off.

注意

當變數被宣告為 Object 時,執行階段類型可以在程式執行時變更。 Visual Basic 會執行在 Object 與值類型間轉換且稱為 boxingunboxing 的作業,所以執行會變慢。 如需 boxing 與 unboxing 的資訊,請參閱 Visual Basic Language Specification

類型推斷會套用在程序層級,不會套用在類別、結構、模組或介面中的程序之外。

如需其他資訊,請參閱區域型別推斷

Option Infer 陳述式不存在時

如果原始程式碼不包含 Option Infer 陳述式,即使用編譯頁面、專案設計工具 (Visual Basic) 上的 Option Infer 設定。 如果使用命令列編譯器,即使用 [-optioninfer] 編譯器選項。

在 IDE 中設定 Option Infer

  1. 在方案總管中選取專案。 按一下 [專案] 功能表上的 [屬性]

  2. 按一下 [編譯] 索引標籤。

  3. Option Infer 方塊中設定值。

當您建立新專案時, 編譯 索引標籤上的 Option Infer 設定會設定為 VB 預設值 對話方塊中的 Option Infer 設定。 若要存取 VB 預設值 對話方塊,請在 [工具] 功能表上,按一下 [選項]。 在 [選項] 對話方塊中,展開 [專案和方案],然後按一下 [VB 預設值]VB 預設值 中的初始預設設定為 On

在命令列上設定 Option Infer

vbc 命令中包含 -optioninfer 編譯器選項。

預設資料類型和值

下表說明在 Dim 陳述式中指定資料類型和初始設定式的各種組合結果。

指定了資料類型? 指定了初始設定式? 範例 結果
No Dim qty 如果 Option Strict 已關閉 (預設值),此變數會設定為 Nothing

如果 Option Strict 已開啟,就會發生編譯時間錯誤。
No Yes Dim qty = 5 如果 Option Infer 已開啟 (預設值),此變數會採用初始設定式的資料類型。 請參閱區域類型推斷

如果 Option Infer 已關閉,且 Option Strict 也已關閉,此變數會採用 Object 的資料類型。

如果 Option Infer 已關閉,但是 Option Strict 已開啟,就會發生編譯時間錯誤。
No Dim qty As Integer 變數會初始化為資料類型的預設值。 如需詳細資訊,請參閱 Dim 陳述式
Yes Yes 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

另請參閱