Edit

Share via


Option Infer Statement

Enables the use of local type inference in declaring variables.

Syntax

VB
Option Infer { On | Off }

Parts

Term Definition
On Optional. Enables local type inference.
Off Optional. Disables local type inference.

Remarks

To set Option Infer in a file, type Option Infer On or Option Infer Off at the top of the file, before any other source code. 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.

When you set Option Infer to On, you can declare local variables without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression.

In the following illustration, Option Infer is turned on. The variable in the declaration Dim someVar = 2 is declared as an integer by type inference.

The following screenshot shows IntelliSense when Option Infer is on:

Screenshot showing IntelliSense view when Option Infer is on.

In the following illustration, Option Infer is turned off. The variable in the declaration Dim someVar = 2 is declared as an Object by type inference. In this example, the Option Strict setting is set to Off on the Compile Page, Project Designer (Visual Basic).

The following screenshot shows IntelliSense when Option Infer is off:

Screenshot showing IntelliSense view when Option Infer is off.

Note

When a variable is declared as an Object, the run-time type can change while the program is running. Visual Basic performs operations called boxing and unboxing to convert between an Object and a value type, which makes execution slower. For information about boxing and unboxing, see the Visual Basic Language Specification.

Type inference applies at the procedure level, and does not apply outside a procedure in a class, structure, module, or interface.

For additional information, see Local Type Inference.

When an Option Infer Statement Is Not Present

If the source code does not contain an Option Infer statement, the Option Infer setting on the Compile Page, Project Designer (Visual Basic) is used. If the command-line compiler is used, the -optioninfer compiler option is used.

To set Option Infer in the IDE

  1. In Solution Explorer, select a project. On the Project menu, click Properties.

  2. Click the Compile tab.

  3. Set the value in the Option infer box.

When you create a new project, the Option Infer setting on the Compile tab is set to the Option Infer setting in the VB Defaults dialog box. To access the VB Defaults dialog box, on the Tools menu, click Options. In the Options dialog box, expand Projects and Solutions, and then click VB Defaults. The initial default setting in VB Defaults is On.

To set Option Infer on the command line

Include the -optioninfer compiler option in the vbc command.

Default Data Types and Values

The following table describes the results of various combinations of specifying the data type and initializer in a Dim statement.

Data type specified? Initializer specified? Example Result
No No Dim qty If Option Strict is off (the default), the variable is set to Nothing.

If Option Strict is on, a compile-time error occurs.
No Yes Dim qty = 5 If Option Infer is on (the default), the variable takes the data type of the initializer. See Local Type Inference.

If Option Infer is off and Option Strict is off, the variable takes the data type of Object.

If Option Infer is off and Option Strict is on, a compile-time error occurs.
Yes No Dim qty As Integer The variable is initialized to the default value for the data type. For more information, see Dim Statement.
Yes Yes Dim qty As Integer = 5 If the data type of the initializer is not convertible to the specified data type, a compile-time error occurs.

Example 1

The following examples demonstrate how the Option Infer statement enables local type inference.

VB
' 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

Example 2

The following example demonstrates that the run-time type can differ when a variable is identified as an Object.

VB
' 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

See also