InfoQ

News

Var/Option Infer: New Syntax for C# and VB 9

Posted by Jonathan Allen on Oct 16, 2006 07:00 AM

Community
.NET
Topics
Artifacts & Tools,
.NET Framework
Tags
Visual Basic.NET,
C#,
Language Features

Microsoft is considering several new language features for C# 3.0 and Visual Basic 9 including type inference. As this may result in breaking changes, a new mode called Option Infer is also being considered for VB.

With Option Infer turned on, VB developers will be able to use type inference for variable declarations. Type inference allows developers to omit type declarations without resorting to late binding. This reduces the amount of code while not giving up the advantages of early (a.k.a. static) binding and compile-time type checking.

Consider this code:

Dim name As String = "John Doe"
Dim age As Integer = 24
Dim gender As Gender = Gender.Male
Dim weight As Decimal = 1.8

In each line, the compiler knows what type the variable is. Unfortunately, developers still have to explicitly type it out to if they want to use strict type checking. With VB 9, developers may be able to save a bit of code by writing this:

Dim name = "John Doe"
Dim age = 24
Dim gender = Gender.Male
Dim weight = 1.8

With Option Infer off, each of these variables will be compiled as type Object. When Option Infer is turned on, the compiler will determine the correct type based on the r-value. If a wider or narrower type is needed, developers can still override this behavior using the As clause.

This also gives rise to a new use of the Dim keyword in For-Each loops.

For Each Dim employee In EmployeeList
 Console.WriteLine(employee.FullName)
Next

While the syntax looks a little awkward, it does allow one to omit the loop variable's type when using strongly typed enumerators. As an additional bonus, changing the contained in EmployeeList does not necessarily mean every for-each loop that accesses it will need to be altered.

C# will also gain type inference, but the syntax is slightly different to account for its C ancestry. A new keyword, var, is used in place of the explicit type.

var name = "John Doe";
var age = 24;
var gender = Gender.Male;
var weight = 1.8;
foreach (var employee in EmployeeList) {
 Console.WriteLine(employee.FullName);
}

This will not result in a breaking change for most C# applications and thus will not need a compiler option. A warning will appear if the application uses a type named "var".

A preview of Visual Studio is available via Virtual PC image. Currently it doesn't support all of the proposed language enhancements, so some of these examples may not in the latest CTP.

No comments

Reply

Exclusive Content

Typemock: Past, Present and Future

Eli Lopian of Typemock answers a few questions on Typemock origins and where Typemock is headed.

Agile in Practice: What Is Actually Going On Out There?

Scott Ambler talks about actual data resulting from surveys made during 2006-2008, showing how Agile is perceived and implemented within organizations.

Building Smart Windows Applications

From QCon 2008, Daniel Moth presents on using Visual Studio 2008 and .NET 3.5 to create compelling rich Windows applications.

Joshua Kerievsky about Industrial XP

Joshua Kerievsky, founder of Industrial Logic, talks about Industrial Extreme Programming which extends XP by including practices dealing with management, customers and developers.

Jeff Barr Discusses Amazon Web Services

Amazon Web Services (AWS) Evangelist Jeff Barr discusses SimpleDB, S3, EC2, SQS, cloud computing, how different Amazon services interact, origins of AWS, AWS globalization and the March AWS outage.

More Than Just Spin (Up) : Virtualization for the Enterprise and SaaS

Cloud services have helped bring virtualization to the forefront. Its full power however, also includes other benefits such as high availability, disaster recovery, and rapid provisioning.

Ruby Beyond Rails

John Lam talks about his path to dynamic languages, some of the problems of making IronRuby run fast, and how the DLR helps with implementing languages.

VMware Infrastructure 3 Book Excerpt and Author Interview

VMware Infrastructure 3: Advanced Technical Design Guide and Advanced Operations Guide provides a wealth of practical insights into setting up virtualization in todays corporate environments.