區域類型推斷 (Visual Basic)Local Type Inference (Visual Basic)

Visual Basic 編譯器會使用型別推斷來判斷未使用 As 子句所宣告的本機變數資料類型。The Visual Basic compiler uses type inference to determine the data types of local variables declared without an As clause. 編譯器會從初始化運算式的類型推斷變數的類型。The compiler infers the type of the variable from the type of the initialization expression. 這可讓您宣告變數,而不需要明確陳述型別,如下列範例所示。This enables you to declare variables without explicitly stating a type, as shown in the following example. 做為宣告的結果,num1num2 都是強型別做為整數。As a result of the declarations, both num1 and num2 are strongly typed as integers.

Public Sub inferenceExample()

    ' Using explicit typing.
    Dim num1 As Integer = 3

    ' Using local type inference.
    Dim num2 = 3

End Sub

注意

如果您不想要將上一個範例中的 num2 輸入 Integer,您可以使用 Dim num3 As Object = 3Dim num4 As Double = 3之類的宣告來指定另一個型別。If you do not want num2 in the previous example to be typed as an Integer, you can specify another type by using a declaration like Dim num3 As Object = 3 or Dim num4 As Double = 3.

注意

型別推斷只能用於非靜態的區域變數;它不能用來判斷類別欄位、屬性或函數的類型。Type inference can be used only for non-static local variables; it cannot be used to determine the type of class fields, properties, or functions.

區欄位型別推斷適用于程式層級。Local type inference applies at procedure level. 它不能用來宣告模組層級的變數(在類別、結構、模組或介面中,而不是在程式或區塊內)。It cannot be used to declare variables at module level (within a class, structure, module, or interface but not within a procedure or block). 如果上一個範例中的 num2 是類別的欄位,而不是程式中的區域變數,則宣告會造成 Option Strict 的錯誤,並將 num2 分類為 Option Strict 關閉的 ObjectIf num2 in the previous example were a field of a class instead of a local variable in a procedure, the declaration would cause an error with Option Strict on, and would classify num2 as an Object with Option Strict off. 同樣地,區欄位型別推斷不適用於宣告為 Static的程式層級變數。Similarly, local type inference does not apply to procedure level variables declared as Static.

型別推斷與晚期繫結的比較Type Inference vs. Late Binding

使用型別推斷的程式碼類似依賴晚期繫結的程式碼。Code that uses type inference resembles code that relies on late binding. 不過,類型推斷會強型別變數,而不是將它保留為 ObjectHowever, type inference strongly types the variable instead of leaving it as Object. 編譯器會在編譯時期使用變數的初始化運算式來判斷變數的類型,以產生早期繫結程式碼。The compiler uses a variable's initializer to determine the variable's type at compile time to produce early-bound code. 在上述範例中,num2(例如 num1)會輸入為 IntegerIn the previous example, num2, like num1, is typed as an Integer.

早期繫結變數的行為與晚期繫結變數的行為不同,只有在執行時間才會知道該類型。The behavior of early-bound variables differs from that of late-bound variables, for which the type is known only at run time. 早期瞭解型別可讓編譯器在執行之前找出問題、精確配置記憶體,以及執行其他優化。Knowing the type early enables the compiler to identify problems before execution, allocate memory precisely, and perform other optimizations. 早期繫結也會啟用 Visual Basic 的整合式開發環境(IDE),以提供有關物件成員的 IntelliSense 協助。Early binding also enables the Visual Basic integrated development environment (IDE) to provide IntelliSense Help about the members of an object. 早期繫結也是效能的慣用選項。Early binding is also preferred for performance. 這是因為儲存在晚期繫結變數中的所有資料都必須包裝為類型 Object,而且在執行時間存取類型的成員會使程式變慢。This is because all data stored in a late-bound variable must be wrapped as type Object, and accessing members of the type at run time makes the program slower.

範例Examples

在未使用 As 子句來宣告區域變數並初始化時,就會發生型別推斷。Type inference occurs when a local variable is declared without an As clause and initialized. 編譯器會使用指派之初始值的類型做為變數的類型。The compiler uses the type of the assigned initial value as the type of the variable. 例如,下列每一行程式碼都會宣告 String類型的變數。For example, each of the following lines of code declares a variable of type String.

' Using explicit typing.
Dim name1 As String = "Springfield"

' Using local type inference.
Dim name2 = "Springfield"

下列程式碼示範兩個對等的方式,以建立整數陣列。The following code demonstrates two equivalent ways to create an array of integers.

' Using explicit typing.
Dim someNumbers1() As Integer = New Integer() {4, 18, 11, 9, 8, 0, 5}

' Using local type inference.
Dim someNumbers2 = New Integer() {4, 18, 11, 9, 8, 0, 5}

使用型別推斷來判斷迴圈控制變數的型別是很方便的。It is convenient to use type inference to determine the type of a loop control variable. 在下列程式碼中,編譯器會推斷 numberInteger,因為上一個範例中的 someNumbers2 是整數陣列。In the following code, the compiler infers that number is an Integer because someNumbers2 from the previous example is an array of integers.

Dim total = 0
For Each number In someNumbers2
    total += number
Next

區欄位型別推斷可以在 Using 語句中使用,以建立資源名稱的類型,如下列範例所示。Local type inference can be used in Using statements to establish the type of the resource name, as the following example demonstrates.

Using proc = New System.Diagnostics.Process
    ' Insert code to work with the resource.
End Using

您也可以從函式的傳回值推斷變數的類型,如下列範例所示。The type of a variable can also be inferred from the return values of functions, as the following example demonstrates. pList1pList2 都是進程陣列,因為 Process.GetProcesses 會傳回進程的陣列。Both pList1 and pList2 are arrays of processes because Process.GetProcesses returns an array of processes.

' Using explicit typing.
Dim pList1() As Process = Process.GetProcesses()

' Using local type inference.
Dim pList2 = Process.GetProcesses()

選項推斷Option Infer

Option Infer 可讓您指定是否允許在特定檔案中使用區欄位型別推斷。Option Infer enables you specify whether local type inference is allowed in a particular file. 若要啟用或封鎖選項,請在檔開頭輸入下列其中一個語句。To enable or to block the option, type one of the following statements at the start of the file.

Option Infer On

Option Infer Off

如果您未在程式碼中指定 Option Infer 的值,則編譯器預設為 Option Infer OnIf you do not specify a value for Option Infer in your code, the compiler default is Option Infer On.

如果在檔案中設定給 Option Infer 的值與 IDE 或命令列中設定的值衝突,檔案中的值具有優先權。If the value set for Option Infer in a file conflicts with the value set in the IDE or on the command line, the value in the file has precedence.

如需詳細資訊,請參閱選項推斷語句編譯頁面、專案設計工具(Visual Basic)For more information, see Option Infer Statement and Compile Page, Project Designer (Visual Basic).

請參閱See also