Udostępnij za pośrednictwem


Option Infer Statement

Enables the use of local type inference in declaring variables.

Option Infer { On | Off }

Parts

  • On
    Optional. Enables local type inference.

  • Off
    Optional. Disables local type inference.

Note

If you do not specify On or Off, the default is On for projects created in Visual Basic 2008. The default is Off for projects upgraded from earlier versions.

Remarks

When you set Option Infer to On, you can declare variables without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression. For example, with Option Infer and Option Strict turned off, the variable in the declaration Dim someVar = 2 is identified only as an object.

IntelliSense when Option Infer and Option Strict are off

IntelliSense view of the declaration.

When you set Option Infer to On, the compiler identifies someVar as an Integer.

IntelliSense when Option Infer is on

IntelliSense view of the declaration.

The compiler can then detect inconsistencies in the use of the variable in your project that otherwise might not have been recognized until the project was executed. Identification of someVar as an Integer also enables the integrated development environment (IDE) to provide full IntelliSense support.

Note

If you do not specify a value for Option Infer in your code or in the IDE, the compiler default is Option Infer On for newly created projects. The default is Option Infer Off for upgraded projects.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

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.

To set Option Infer in the IDE for a single project

  1. Click a project in Solution Explorer.

  2. Open the Project Designer by clicking Property Pages on the View menu.

  3. On the Compile tab, in the Option Infer box, click On or Off.

To set the default value for Option Infer in the IDE

  1. On the Tools menu, click Options.

  2. Expand the Projects and Solutions node.

  3. Click VB Defaults.

  4. In the Option Infer list, click On or Off.

    Note

    If you set the value for Option Infer by using the Tools menu, the value will persist through subsequent projects unless you change it.

To set Option Infer on the command line

  • Include the /optioninfer compiler option in the vbc command.

Example

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

' 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 

' If CustomerList is a list of Customer objects, 
' variable cust is an instance of the Customer class. 
For Each cust In CustomerList
    Console.WriteLine(cust.Name)
Next 

' Variable namedCust is an instance of the Customer class. 
Dim namedCust = New Customer With {.Name = "Lance Tucker", _
                                   .City = "Seattle"}

' Variable product is an instance of an anonymous type. 
Dim product = New With {Key .Name = "paperclips", Key .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

See Also

Concepts

Local Type Inference

Reference

Dim Statement (Visual Basic)

Option Compare Statement

Option Explicit Statement (Visual Basic)

Option Strict Statement

Visual Basic Defaults, Projects, Options Dialog Box

/optioninfer