Automatic Properties, Collection Initializers, and Implicit Line Continuation support with VB 2010
[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu]
This is the eighteenth in a series of blog posts I’m doing on the upcoming VS 2010 and .NET 4 release.
A few days ago I blogged about two new language features coming with C# 4.0: optional parameters and named arguments.
Today I’m going to post about a few of my favorite new features being added to VB with VS 2010: Auto-Implemented Properties, Collection Initializers, and Implicit Line Continuation support.
Auto-Implemented Properties
Prior to VB 2010, implementing properties within a class using VB required you to explicitly declare the property as well as implement a backing field variable to store its value.
For example, the code below demonstrates how to implement a “Person” class using VB 2008 that exposes two public properties - “Name” and “Age”:
While explicitly declaring properties like above provides maximum flexibility, I’ve always found writing this type of boiler-plate get/set code tedious when you are simply storing/retrieving the value from a field. You can use VS code snippets to help automate the generation of it – but it still generates a lot of code that feels redundant. C# 2008 introduced a cool new feature called automatic properties that helps cut down the code quite a bit for the common case where properties are simply backed by a field. VB 2010 also now supports this same feature.
Using the auto-implemented properties feature of VB 2010 we can now implement our Person class using just the code below:
When you declare an auto-implemented property, the VB compiler automatically creates a private field to store the property value as well as generates the associated Get/Set methods for you. As you can see above – the code is much more concise and easier to read.
The syntax supports optionally initializing the properties with default values as well if you want to:
You can learn more about VB 2010’s automatic property support from this MSDN page.
Collection Initializers
VB 2010 also now supports using collection initializers to easily create a collection and populate it with an initial set of values. You identify a collection initializer by declaring a collection variable and then use the From keyword followed by braces { } that contain the list of initial values to add to the collection.
Below is a code example where I am using the new collection initializer feature to populate a “Friends” list of Person objects with two people, and then bind it to a GridView control to display on a page:
You can learn more about VB 2010’s collection initializer support from this MSDN page.
Implicit Line Continuation Support
Traditionally, when a statement in VB has been split up across multiple lines, you had to use a line-continuation underscore character (_) to indicate that the statement wasn’t complete.
For example, with VB 2008 the below LINQ query needs to append a “_” at the end of each line to indicate that the query is not complete yet:
The VB 2010 compiler and code editor now adds support for what is called “implicit line continuation support” – which means that it is smarter about auto-detecting line continuation scenarios, and as a result no longer needs you to explicitly indicate that the statement continues in many, many scenarios. This means that with VB 2010 we can now write the above code with no “_” at all:
The implicit line continuation feature also works well when editing XML Literals within VB (which is pretty cool).
You can learn more about VB 2010’s Implicit Line Continuation support and many of the scenarios it supports from this MSDN page (scroll down to the “Implicit Line Continuation” section to find details).
Summary
The above three VB language features are but a few of the new language and code editor features coming with VB 2010. Visit this site to learn more about some of the other VB language features coming with the release.
Also subscribe to the VB team’s blog to learn more and stay up-to-date with the posts they the team regularly publishes.
Hope this helps,
Scott