Export (0) Print
Expand All

Local Type Inference

Updated: July 2008

The compiler in Visual Basic 2008 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.

Public Sub inferenceExample()

    ' Using explicit typing. 
    Dim num1 As Integer = 3

    ' Using local type inference. 
    Dim num2 = 3

End Sub

Local type inference cannot be used to declare class fields. If num2 in the previous example were a field instead of a local variable, the declaration would cause an error with Option Strict on, and would classify num2 as an Object with Option Strict off. Similarly, the types of static variables cannot be inferred when Option Strict is on. With Option Strict off, the type of staticVar in Static staticVar = 5 is Object.

Code that uses type inference resembles code that relies on late binding. However, 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. In the previous example, num2 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. 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. 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.


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 occurs when a local variable is declared without an As clause and assigned a value. The compiler uses the type of the value as the type of the variable. For example, the following lines of code each declare 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}

You can use type inference to determine the type of a loop control variable. In the following code, the compiler infers that number is an Integer, because someNumbers2 is an array of integers.

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

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. Both pList1 and pList2 are lists of processes.

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

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

A new option, Option Infer, lets you specify whether local type inference is allowed in a particular file. To enable or block the option, type one of the following statements at the start of the file.

Option Infer On

Option Infer Off

If you do not specify a value for Option Infer in your code, the compiler default is Option Infer On for projects created in Visual Basic 2008, and Option Infer Off for projects upgraded from earlier versions. For more information, see Option Infer Statement and /optioninfer.


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.

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.




July 2008

Changed the name of the loop control variable in the text in the "Examples" section from num to number to match the name in the code.

Customer feedback.

Community Additions

© 2014 Microsoft