區域類型推斷 (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 = 3If 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作為ObjectOption Strict關閉。If 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. 同樣地,區域類型推斷並不適用於程序層級變數宣告為StaticSimilarly, local type inference does not apply to procedure level variables declared as Static.

類型推斷 vs。晚期繫結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. 例如,下列程式碼行的每個宣告類型的變數StringFor 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 InferOption 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.

如需詳細資訊,請參閱 < Option Infer 陳述式編譯的 Page,Project Designer (Visual Basic)For more information, see Option Infer Statement and Compile Page, Project Designer (Visual Basic).

另請參閱See also