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:

image

As you can see, the code doesn’t specify the type for all variables because the compiler can infer the most appropriate data type according to the usage of a variable. To ensure that the VB compiler inferred ...

Get Visual Basic® 2010 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.