Tuesday, January 5, 2010

New Features in VB.NET

In this blog-post, I’m going to talk about new language or framework features introduced in .NET Fx 4.0 with VB.NET – to be precise.

With the release of VS2008 and .NET Fx 3.5, there were lot of new language enhancements and features extensions to framework, like LINQ, Extension methods, Lambda expressions, Expression trees, Auto-Implemented properties, Object and Collection initializer. However, some of these features were not common to C# and VB. Like Auto-Implemented Properties were not there in VB, but were available only in C#. You can find my blog-post Language Enhancements in C# 3.0 and VB9, which I had written, when VS2008 was released.

With the advent of Visual Studio 2010, .NET Framework 4.0, now these languages will compliment each other. Almost all language features are now common to these languages. Let’s visit some of these new features.
  1. Implicit Line Continuation [In VB only]:
    In VB, each statement ends at the end of line. However, in previous versions of VB, if developers wanted to part the code in multiple lines, they had to use (_) character. Following code snippet will show you this. But, with this release of VB.NET, now developers can write code in multiple lines, without requiring any special character. The code can be written easily as:
    Untitled-2
    However, there are some rules. Developers can’t just start a new line from anywhere in between the code. The next line can be started:
    After a comma, after open parenthesis like “(“ or before closing parenthesis “)”.
    You can find more information about this here on MSDN
  2. Auto-Implemented Property [In VB, Was there in C#]:
    As MSDN says: Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. So, that means, now I don’t need to create a private variable don’t need to write getter and setter to ‘encapsulate’ it.
    Visual Basic compiler will automatically create this private variable for you. So, you can create a Auto-Implemented property by writing:
    Untitled-3 
    So, one may ask, what will be the name of this private  variable and can I access it? Well, the private variable, also called as ‘backing field’, will have same name as that of property, with underscore (_) prefixed. This field can be accessed in code and in debug window, but will not be available in IntelliSense.
    For more information, visit: MSDN
  3. Collection Initializer [In VB.NET, Was there in C#]:
    In C# 3.0, we had collection initializer. It is nothing but a shor-cut to create and initialize the collection. So, if I use VS1010 with new features in VB.NET, now I can write it like:
    sol2
    And this will create a one dimensional array of strings. So, this is just one another shortest method to create and initialize arrays and collections.
    You can find other useful information here at MSDN
So like, there are some more features to framework, which includes addition of few more classes to Base Class Library (BCL), some enhancements to Garbage Collector, and Dynamic Language support. We’ll see these features soon.

Till then, enjoy coding.

6 comments: