Infer a Variable’s Data Type Based on Assignment

In the later versions of Visual Basic and C# (2008 and later), 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 actually infer the data type you intend to use based on your code. This process is called local type inference or implicit typing.

For 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 ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition 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.