July 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Language Feature Highlight: Local Type Inference in C# 3.0 and Visual Basic 9.0

  • November 14, 2007
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Local Type Inference in Visual Basic 9.0

Using local type inference in Visual Basic is more likely to be misleading because it can appear as if it matches the behavior of Option Strict Off. As with C#, you still get a strong type despite the appearances. The syntax in Visual Basic is to omit the As Type part of the variable declaration. This will work just fine with Option Strict On and it is encouraged to continue use of Option Strict On to ensure variant types are not allowed and avoids the undesired boxing and unboxing.

Visual Basic allows you to turn local type inference on and off. There is Option Infer On and Option Infer Off that will enable or disable local type inference respectively. It is on by default.

The sample code below will demonstrate the syntax for local type inference in Visual Basic.

Option Strict OnModule Module1   Sub Main()      Dim As Integer = 5      Dim b = a             ' int      Dim x = 5.5           ' double      Dim s = "string"      ' string      Dim l = s.Length()    ' int      Console.WriteLine("value of b is {0} and type is {1}", b, _         b.GetType().ToString())      Console.WriteLine("type of x is {0}",         x.GetType().ToString())      Console.WriteLine("type of s is {0}",         s.GetType().ToString())      Console.WriteLine("type of l is {0}",         l.GetType().ToString())      Console.ReadLine()   End SubEnd Module

As with the C# example, it can be just as beneficial to demonstrate where local type inference cannot be used. The following sample code demonstrates invalid attempts at using local type inference in Visual Basic 9.0. It is important to note this code will not compile.

Module Module1   Dim test = "invalid use"    ' invalid in member declaration   ' Invalid as parameter   Sub TryAsParameter(ByVal parm)   End Sub   Sub Main()      Dim local                 ' must be initialized      Dim local3 = 5            ' valid use      local3 = "change type"    ' cannot change type   End SubEnd Module

Summary

This article covered the local type inference language feature. You examined what it is along with the syntax using examples of how it can and cannot be used. Type inference is one of the language features along with extension methods, partial methods, and others that enable Language Integrated Queries (LINQ) to be possible, so it is important to understand how type inference works and what it is not.

Future Columns

The topic of the next column is yet to be determined. It is likely I will cover another upcoming language feature. If you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fourth year in a row. You can reach Mark at mstrawmyer@crowechizek.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel