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.
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.
The following code demonstrates two equivalent ways to create an array of integers.
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.
Local type inference can be used in Using statements to establish the type of the resource name, as the following example demonstrates.
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.
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.