In the latest versions of Visual Basic and C#, you can define variables without explicitly setting their data type. And, when doing so, you can still get the benefits of strongly typed variables (compiler checking, memory allocation, and more). The compilers have been improved to actually infer the data type you intend to use based on your code. This process is called local type inference, or implicit typing.

As an example, consider the following lines of code. Here you create a variable of type

String and assign a value.

C#

string companyName = “Contoso”;

VB

Dim companyName As String = “Contoso”

Now, let’s look at the same line of code using type inference. You can see that you do not need the string portion of the declaration. Instead, the compiler is able to determine that you want a string and strongly type the variable for you. In C#, this is triggered by the new keyword, var. This should not be confused with the var statement in languages such as JavaScript. Variables defined as var will be strongly typed. In VB, you still simply use the Dim statement but omit the data type.

C#

var companyName = “Contoso”;

VB

Dim companyName = “Contoso”

These two lines of code are equivalent in all ways. Although in the second example no data type was declared, one is being declared by the compiler. This is not a return to a generalized data type such as Variant or Object. Nor does this represent late-binding of the variable. Rather, it is simply a smarter compiler that strongly types the variable by choosing a data type based on the code. You get all the benefits of early-bound variables while saving some keystrokes.

There are a few things for you to be aware of when using type inference. The first is that it requires your local variable to be assigned a value in order to do the compiler typing. This should not be a big deal because if your variable is not assigned it is not used.

The second item you should consider is that type inference works only with local types. It does not work with class-level variables (also called fields) or static variables. In these cases, using local type inference will result in an error being thrown by the compiler in C#. In VB, you would get the same error provided that Option Strict is set to On. If you are not using Option Strict in your VB code, the variable will not be strongly typed. Instead, the variable will be assigned the generic, Object data type.

Local type inference can be useful in other declaration scenarios as well. This includes defining arrays, creating variables during looping, defining a variable inside a Using statement, and defining a variable that contains the result of a function call. In each of these cases, the compiler can infer your data type based on the context of the code. As another example, the following code creates a Using statement and infers the type of the variable cnn.

C#

using (var cnn = new System.Data.SqlClient.SqlConnection()) {

//code to work with the connection

}

VB

Using cnn = New System.Data.SqlClient.SqlConnection

‘code to work with the connection

End Using

In Visual Basic, you can turn local type inference off and on for a given file. By default, a new VB code file is set to allow type inference. However, if you want to turn it off at the file level, you can do so by setting Option Infer Off at the top of the code file.

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner