Local Type Inference

Local type inference is a language feature that allows you to omit specifying the data type of a local variable even if Option Strict is set to On. The Visual Basic compiler can deduce (infer) the most appropriate data type depending on the variable’s usage. The easiest way to understand and hopefully appreciate local type inference is to provide a code example. Consider the following code and pay attention to the comments:

Sub Main()    'The compiler infers String    Dim oneString = "Hello Visual Basic 2015!"    'The compiler infers Integer    Dim oneInt = 324    'The compiler infers Double    Dim oneDbl = 123.456    'The compiler infers Boolean    Dim oneBool = TrueEnd Sub

As you can see, ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.