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
.NET Framework ,
Artifacts & Tools
Tags
Language Features ,
Visual Basic.NET ,
C#

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

Watch Thread Reply

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.