Local type inference (Visual Basic)
Visual StudioOther versions
The Visual Basic compiler uses type inference to determine what is not usedThe data type of the local variable declared by the AS clause. The compiler infers the type of the variable by the type of the initialization expression. This allows you to declare a variable without explicitly declaring the type, as shown in the following example. The result of the declaration is thatboth NUM1 and num2 are strongly typed as integers.
Vb
Public Sub inferenceexample () ' Using explicit typing. Dim NUM1 as Integer = 3 ' Using local type inference. Dim num2 = 3End Sub
| Description |
If you do not want the num2 in the preceding example to be typed as Integer, you can specify another type with a declaration (such as dim num3 As Object = 3 or Dim num4 As Double = 3). |
Local type inference is applied at the process level.It cannot be used to declare variables at the module level (within a class, struct, module, or interface, but not within a procedure or block).If the above examplenum2 is a field of a class and not a local variable in a procedure, the declaration causes an error when option Strict is opened, and the declaration classifies num2 as Object when option Strict is closed . Similarly, local type inference is not applied to process-level variables declared as Static.
type inference and late binding
Code that uses type inference is similar to code that relies on late binding.object. " However, type inference can set a variable to a strongly typed rather than keeping the variable object. num2, like num1, was typed as an integer." In the previous example, as with num1 , num2 was typed as integer.
Early bound variables behave differently from late-bound variables, and only the types of late-bound variables are known at run time.Knowing the type at an early stage allows the compiler to identify problems before execution, allocate memory accurately, 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 can also help achieve higher performance. This is because all data stored in a late-bound variable must be wrapped as type Object, and accessing members of that type at run time can make the program run slower.
Example
Type inference occurs when a local variable is not declared with the AS clause and is initialized. the compiler assigns the type of the initial value as the type of the variable. For example, each of the following lines of code declares a variable of type String.
Vb
' Using explicit typing. Dim name1 as String = "Springfield" ' Using local type inference. Dim name2 = "Springfield"
The following code shows two equivalent ways of creating an array of integers.
Vb
' Using explicit typing. Dim someNumbers1 () as Integer = New Integer () {4, 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 the loop control variable. In the following code, the compiler infers that number is an integer because the someNumbers2 in the previous example is an array of integers.
Vb
Dim total = 0For with someNumbers2 total + = Numbernext
You can use local type inference in a using statement to determine the type of the resource name, as shown in the following example.
Vb
Using proc = New System.Diagnostics.Process ' Insert code to work with the resource. End Using
You can also infer the type of a variable from the return value of the function, as shown in the following example. both PList1 and pList2 are process arrays because process.getprocesses returns an array of processes.
Vb
' Using explicit typing. Dim PList1 () as Process = Process.getprocesses () ' Using local type inference. Dim pList2 = process.getprocesses ()
Option Infer
Option Infer can specify whether local type inference grants permissions in a particular file. to enable or block this option, type one of the following statements at the beginning of the file.
Option Infer on
Option Infer Off
option infer in your code, the compiler default is o Ption infer on. " If &NBSP is not specified in the code, option infer value, the compiler defaults to option infer on. option infer Off. " > for projects upgraded from Visual Basic 2008 or earlier, the compiler defaults to option infer Off.
If the value set for Option infer in the file conflicts with the value set in the IDE or the command line, the value in the file takes precedence.
For more information, see the Option infer statement and the compile page, Project Designer (Visual Basic).
Limit
Type inference can only be used for non-static local variables, and cannot be used to determine the type of a class field, property, or function.
VB is a piece of cake-----understand "type inference"