Visual Studio VB 2010 Enhancements

The latest evolution of VB in Visual Studio will bring with it a bundle of C# functionality, nudging the two languages even closer together. To ensure we're ready to make the most of these enhancements, Hima Vejella takes us through each new feature.


The constant debate between C# and VB developers over which language might be the best is a tough one to resolve, as both of the languages have their own strengths and, when you get down to it, are equally competent. Thus far, the feature split has been an issue in both directions. For example, C# 3.0’s collection initializers feature, which enable it to define the contents of a collection at the same time as declaring it, is not supported in Visual Basic 9.The same is true of auto-implemented properties, which specify property procedures without needing to specify get or set. As a quick example going the other way, Visual Basic supports optional parameters (which can be omitted based on requirement) in methods, whereas C# 3.0 does not.

However, both sides should be interested to hear that Microsoft is now trying to have all the features and strengths common to both the languages. As a quick example, C# 4.0 now supports optional parameters (as described above) as well as named parameters ( which are declared with default values), but that’s just scratching the surface. The recent merging of the Visual Basic and C# teams will hopefully help to ensure that both languages grow and evolve together, and Visual Basic has already been updated to include a number of C# features that it was sorely missing. In this article, I’ll give you an overview of all the features, which have been added as part of VB 2010 and .NET4 in the Visual Studio 2010 IDE, and give some quick demonstrations of how they’ll save developers time, and allows us to be more productive at the same time as writing more elegant code.

1. Using implicit line continuation

To start with, say goodbye to the underscore which needed to be inserted after each connected line in previous versions of Visual Studio. Now the VB 2010 compiler can recognize the ‘_’ character as being implicit, so we can use it:

  • Within XML literals(before or after an embedded <%= or %>expression), LINQ  Queries and Query operators;
  • After open parentheses, open curly brace attributes, Commas ,binary operators, Is and Is Not operators;
  • Before closing parentheses and closing curly brace braces;
  • After the assignment operator (“=”), which you can find a more detailed explanation of on MSDN.

This feature has been one of the top requests from VB.NET developers, and thankfully we now don’t need to have underscored characters for the line breaks to integrate:

However, bear in mind that this only works in the circumstances I’ve mentioned above, so the code below is still a compiler error, and is not a recommended convention, anyway:


Figure 1. Implicit line continuation won’t save you from all your compiler errors.

As the underscore has not been removed from every single place in VB, this is a language feature which should actually be considered as an IDE enhancement. In any case, the moment we press enter, the control is now aligned to the end of the parameter, which is much more user-friendly.

2. Auto – Implemented Properties

Auto-implemented properties is a feature which was available in C# 3.0, and, in case you’re not aware of it,  offers us a way to specify a property of a class without having to write the code to Get and Set it; it’s basically the ability to declare a property without declaring a separate field to store the value. The automatically-implemented properties work the same as if the developer had declared a private field to hold the data and implement simple “getters” as well as “setters”, and are called in exactly the same way as traditional properties, as well as being a replacement of property procedures. If a more complex logic is required for property access, then traditional properties, which store data in private fields, can be created as well. Here’s an example of auto-implemented properties:

To clearly illustrate this new language feature, in case you’re not familiar with Visual Basic, I’ll show you the equivalent of the code above, as written in older versions of Visual Studio:

We can see that not only are three properties implemented in just 3 lines of code, but the code maintainability and overhead of declaring and implementing property procedures has now become significantly easier.

Auto- Implemented properties with default values

As part of this elegant and powerful new functionality, we can declare a property with a default value in a single line; for example:

Also based on the feedback from Visual Basic developers, this feature has been made available in Visual Studio 2010 to save time and improve productivity, so the previously laborious property implementations can now be replaced with simple, one-line declarations.

3. Collection Initializers

Collection Initializers enable developers to save significant amounts of time by declaring and then directly initializing list contents, without the need through the long process of instantiating the list, individually “newing up” each item, and then adding the items to the list. Collection initializers were another C# 3.0 feature which has now been brought across to VB10, and here’s an example of how we can use them to create a collection and populate it with an initial set of values:

Of, if you’d like a slightly more involved example:

As you can see, we can simply create a collection of List (Employee), and each value which is supplied in the collection initializer is passed to the corresponding Add method of the collection. This functionality is not just limited to lists, but could also be used to create something like a dictionary (just as an example). Jason Olson gives some great examples of what this new functionality can achieve in his“10-4” episode on Channel 9.

4. Array Literals

Array literals in Visual Basic 10.0 provide a shortcut mechanism for declaring and filling arrays of types that can be inferred by the compiler. Examples are as below:

Now the question is, what will the compiler infer if the data types are mixed?

In the above example, the compiler infers the datatype as double, and does so by examining each array element datatype and using the dominant datatype to make its inference. Consider the below example; what if string and number datatypes are mixed equally?

Unsurprisingly, if we mix these datatypes, the compiler cannot make any specific inferences without the risk of potentially loosing the data due to narrow type conversion of string to integer or vice versa.


Figure 2. The compiler is able to handle arrays containing mixed datatypes by inferring them to be Objects.

Hence the entire array is treated as an Object, although it’s still recommended to use types in isolation with respect to a individual arrays. Also, in the example in figure 2, the compiler will generate a warning if OPTION STRICT is ON, and will only attempt to infer the type of objects if OPTION STRICT is OFF.

In addition to this inference functionality, we can also use nested array literals to create multidimensional arrays as below:


Figure 3. A MultiDimensional array, created using array literals


Figure 4. A Jagged array, created using array literals

To give you a concrete example of how this will slim down your code, for an array declaration of something like Employee Salary, we would previously have used the following code:

However, with the help of the array literals which we’ve just learnt about, the same code can now be written more simply as:

We can also declare auto-implemented properties using array literals:

5. Multiline Lambdas

We can now pass values from the current scope to a Lambda expression, which is a single or multi line anonymous function inside another function, used wherever a delegate is valid. A multiline lambda function is simply a lambda expression representing a function containing one or more statements. These provide developers with an elegant way of expressing logic locally, rather than splitting it out across several methods.

Creating and Assigning a Multiline Lambda

Below is an example of a multiline lambda function, created and assigned to a variable. As mentioned earlier, it does not require line continuation characters, as these are now implicit in VB 10.0

The compiler will infer the parameters and return types where possible, just like in regular lambdas. Moreover, whenever we refer to the declared lambda expression variable, the lambda expression is invoked, and the “Function” keyword is responsible for creating a multiline lambda that returns a value.

Expression Statements

Visual Basic 9.0 required inline lambda expressions to return a value, such that, for example, the following line would throw an error, as it does not support expressions that returned a value:

Visual Basic 10.0 now comes with better support for lambdas, and they now can contain expressions that don’t return a value, as the Sub keyword indicates below:

If you’re wondering how you might put this new functionality to use, say we have an array of employee salaries and we want to print all salaries greater than 40,000.Multiline lambda expression is used here to pass into the EmployeeSal.

A multiline lambda expression is used in conjunction with an if else condition to get string that starts with  ‘H’ or ‘S’  is described in the code below

Bear in mind that Lambda expressions cannot have modifiers, Overloads, Overrides, generic parameters, optional or paramarray parameters. In addition, the data type of a lambda expression parameter can either be inferred or be specified using the As keyword, but we have to either specify all the parameter data types, or they all need to be inferred.

6. Interoperability with dynamic languages

Visual Studio 2010 ships with a new platform called the Dynamic Language Runtime which, as you might have guessed, makes it easier to establish interoperability with dynamic languages such as Python and Ruby. Visual Basic 2010 fully supports the DLR, allowing developers to use libraries and frameworks written in these dynamic languages. For example, the following code snippet calls a method defined in a Python library “”:

7. VB.NET Snippets

There are now additional Code snippets  in VB.NET which significantly speed up development when working with ASP.Net MVC2, LINQ, Designer Patterns, Error Handling, Office Dev, WPF etc. Simply right-click in the code behind file, select Insert Snippet, and then select the snippet that is required. Code snippets can be created by developers and then easily re-used across multiple projects. Alessandro English has created an addin for VS2010 to add XAML code snippets into the Visual Studio 2010 code editor for XAML, when working in WPF and Silverlight projects. There is also a snippet editor available on Codeplex, which can be used for creating and managing code snippets in Visual Studio.


Figure 5. A demonstration of the code snippets functionality

8. Parallel Support

Visual Studio 2010 and the .NET Framework 4 now provide a runtime, new class library types and analytic tools which fully support parallel programming. This is an excellent feature, and it allows us to develop applications that utilize multiple CPU Cores, and thus enables multiple threads to be executed simultaneously. This enhancement will allow us to take full advantage of the available modern hardware. This support can be accessed using the Parallel object of the.NET 4.0 Framework, but we need to import the relevant namespace at the start of our code:

In this simple example, the following bit of code (as seen in figure 6.), placed the code inside the Main () routine, will output two strings in parallel on the console.

As you can see, the Invoke () method is used to execute the tasks in parallel, and when the code is run we can see the two strings on the console output screen as below:


Figure 6. Employing the new parallel development functionality to reverse the writing order.

Parallel programming can be debugged and examined clearly with the help of this new VS2010 feature, using the Parallel Tasks and Parallel Stacks windows. If necessary, we can navigate between instances with CTRL+SHIFT+DOWN ARROW or CTRL+SHIFT+UP ARROW. The ability to do parallel development will undoubtedly help us to write elegant, and scalable code without having to work directly with threads or locks.

9. Compiling without PIAs (or Embed Interop Types)

While working with VSTO, using .NET to code an application against the Microsoft Office, or working with any application that performs COM Interop, Object Model Primary Interop Assemblies (PIAs)need to be deployed to end-user machines to allow data to move between COM and .NET. Unfortunately, these assemblies are an additional deployment concern, can cause versioning problems, and are also very large, and so deploying them is often a similarly large headache.

Thankfully, Visual Basic 10 now allows these applications to be deployed without requiring PIAs to exist on the end user’s machine. This is done by generating local types to be responsible for performing the interop calls to the COM library. Even better, these types are inferred by the compiler, so that the Common Language Runtime (CLR) can identify them across assemblies, and only the PIA types which are absolutely necessary will be copied into the assemblies.


Figure 7. Accessing the properties of the project’s PIA reference


Figure 8. Adjusting the Embed Interop Types of the PIA reference

This can be achieved for existing projects by setting the Embed Interop Types property to true in the project’s Solution Explorer reference, although this property is set to True by default for new references. Simply open Solution Explorer, select the PIA reference in the project, Right-click  on the reference and select properties of the reference , and then set Embed Interop Types to True in the window which will appear.

10. Optional Parameters can be Nullable

A limitation in earlier versions of VB was that optional parameters could not be nullable. Visual Basic 2010 now has the ability to define optional parameters of any value type, so now you can create method with optional parameters and also make them Nullable.

In this case, strName is an optional parameter that is passed with a default value, intSalary is of type Nullable(Of Integer) and intAge is a non-intrinsic type, but they can still be used as optional parameters.

All the optional or default parameters need to be declared at the end of the parameter list, and we can, if we desire, have all the parameters of a method as optional, depending on the requirements we have in mind.

11. Generic Variance

Now say we have a generic List(Of Potato) that will pass to a method that accepts an IEnumerable(Of Vegetable):

Even though the List implements IEnumerable and a Potato inherits from the Vegetable class, this will not work in Visual Studio 2008, but is now supported in Visual Basic 10 and also in C#4.0.  Now we can declare covariant widening and narrowing generic types using the Out and In modifiers. This is because IEnumerable interface in the CLR 4.0 has been changed to designate a covariant generic type, and if you’d like to gain a more elaborate understanding of these concepts, then take a look at this article on Generic Co- and Contravariance.


With features available in C# and VB starting to converge, both languages will now start to evolve in concert, making a wider palette of options available to developers. Thankfully, you don’t have to only be targeting the .NET Framework 4 to be able to employ these latest Visual Basic enhancements, as they can be used in projects that target .NET Framework versions as far back as 2.0. There is, however, one exception to this backwards compatibility: the Embed Interop Types, which unfortunately has a dependency on types which are only available in the .NET Framework 4. That is, I feel, a small price to pay. If you want to enable this multi-targetting, you can do so by navigating to the Advanced Compile Options (double-click on the Project and look under the Compile tab), and selecting your targets from the combinations at the bottom of the window as shown below.


Figure 9. Enabling multi-targetting in VS2010

If you’d like to learn more about any of these new features and enhancements, there’s a detailed write-up of what’s new in Visual Basic 2010, by Jonathan Aneja, available on MSDN.

It’s also good to know that the feature swap is also going in the other direction, such as the support which has been added in C#4.0 for optional and named parameters, two features that are a part of VB 9.0. Now that we’ve seen what’s new in VB 2010, in my next article I will be rounding off the story by discussing the C# 4.0 enhancements added in VS2010.