Type inference refers to the ability of a programming language to automatically deduce a variable’s type. Local type inference is available starting with C# 3.0 and Visual Basic 9.0, which were released in 2007 with version 3.5 of the .NET platform. The feature was added to support anonymous types and the relaxed programming style required by Linq.
Local type inference lets you define and use local variables without specifying their type. These variables are commonly called implicitly typed local variables because the compiler infers a strong data type from the initialization expression.
In Visual Basic, the compiler uses type inference to determine the data type without the "As" clause.
' Using explicit typing
Dim x as Integer = 3
' Using local type inference
Dim x = 3
In C#, you use the var keyword as shown in the following example:
// Using explicit typing
int x = 3;
// Using local type inference
var x = 3;
Here are some things to keep in mind when working with local type inference:
Implicitly typed local variables are not the same as variables defined as objects. They are also not to be confused with variants (used in vb6).
Implicitly typed local variables must be initialized to a specific non-null value with the definition.
Their use is restricted. They are always local in scope. You cannot use them as a parameter in a function or in a member declaration or as a return type.
You can't change the type.
In Visual Basic, local type inference is turned on by default but can be turned off using "Option Infer Off".