Option Infer 陳述式

在宣告變數中啟用區域型別推斷。

Option Infer { On | Off }

組件

詞彙

定義

On

選擇項。 啟用區域型別推斷。

Off

選擇項。 停用區域型別推斷。

備註

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

將 Option Infer 設定為 On 時,不需要明確陳述資料型別便可以宣告區域變數。 編譯器 (Compiler) 會從變數之初始運算式的型別來推斷此變數的資料型別。

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

Option Infer 開啟時的 IntelliSense

宣告的 IntelliSense 檢視。

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

Option Infer 關閉時的 IntelliSense

宣告的 IntelliSense 檢視。

將變數宣告為 Object 時,執行階段型別可能會在程式執行當中變更。 Visual Basic 會執行稱為「boxing」和「unboxing」的作業,以便在 Object 和實值型別之間進行轉換。 這些作業會使執行速度減慢。 如需 Boxing 和 Unboxing 的詳細資訊,請參閱 Visual Basic 語言規格

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

如需詳細資訊,請參閱區域型別推斷 (Visual Basic)

當 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

預設資料型別及值

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

資料型別已指定?

是否已指定初始設定式?

範例

結果

Dim qty

如果 Option Strict 關閉 (預設值),則變數會設定為 Nothing。

如果 Option Strict 開啟,則會發生編譯時期錯誤。

Dim qty = 5

如果 Option Infer 開啟 (預設值),則變數會使用初始設定式的資料型別。 請參閱區域型別推斷 (Visual Basic)

如果 Option Infer 關閉且 Option Strict 關閉,則變數會使用 Object 的資料型別。

如果 Option Infer 關閉而 Option Strict 開啟,則會發生編譯時期錯誤。

Dim qty As Integer

變數會初始化為資料型別的預設值。 如需詳細資訊,請參閱Dim 陳述式 (Visual Basic)

Dim qty As Integer = 5

如果初始設定式的資料型別無法轉換為指定的資料型別,就會發生編譯時期錯誤。

範例

下列範例示範 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

下列範例示範當變數識別為 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

請參閱

參考

Dim 陳述式 (Visual Basic)

Option Compare 陳述式

Option Explicit 陳述式 (Visual Basic)

Option Strict 陳述式

選項對話方塊、專案、Visual Basic 預設值

/optioninfer

概念

區域型別推斷 (Visual Basic)

變更記錄

日期

記錄

原因

2011 年 4 月

已加入有關 Object 型別和型別推斷的資訊。

資訊加強。

2011 年 3 月

已修改備註並加入範例。

資訊加強。