Sideway from Sideway
Draft for Information Only


VB.NET Local ype Inference
 Type Inference vs. Late Binding
 Option Infer
 See also

VB.NET Local ype Inference

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. 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


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 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). 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. Similarly, local type inference does not apply to procedure level variables declared as Static.

Type Inference vs. Late Binding

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, 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. 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.


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. For 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. 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

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

If you do not specify a value for Option Infer in your code, the compiler default is Option Infer On.

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.

For more information, see Option Infer Statement and Compile Page, Project Designer (Visual Basic).

See also




ID: 200900009 Last Updated: 9/9/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 149


Application 198

Digitization 25

Latex 6

Manim 113

Numeric 19


Web 283

Unicode 494


CSS 58


OS 389

DeskTop 7

Python 19



Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2


Electric 27

Biology 1

Geography 1

Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019