cool vb.net 10 features

I would like to introduce you new vb.net 2010 features some of which have been missing since long and developers were always craving for them. These new features are bundled with VB.Net in .NET framework 4.0 and they will surely going to improve developer's productivity and code quality .

Implicit line continuation: You don't need to put underscore character at the end of the line to make compiler understand that this line is extending to next line. Though underscore is still needed in couple of cases for e.g. declaring attribute at the top of function or class. e.g.

Dim s As String = "Hello" &
"World"

is a valid statement. Ofcourse if you want to make it more readable you should still use underscore as continuation

Dim s As String = "Hello" _ 'using underscore otherwise it would be difficult
& "World" 'to understand if line is extended to next line or not


Auto implemented properties: Like C# in VB.NET too you don't need to write get/set functions if they are meant to be used as default. for e.g.

Public Class Student
Property Age As Integer
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class

You can see Property "Age" is auto implemented and we don't need to explicitly write its get/set functions. Internally compiler will automatically generate the fields starting with underscore.


Object initializer: Now like C#, it is possible in vb.net to set object properties during the time of its initialization. We can construct the earlier mentioned Student class object as following

Dim myStudent As New Student With {
.Age = 20,
.Name = "Abhishek"
}


Multiline lambda or Anonymous methods: Earlier vb.net used to support single line anonymous method only which made using lambda or anonymous method bit annoying. you had to write statements something like this
Dim mySub = Sub() Debug.WriteLine("Difficult to write complex implementation")

but now with vb.net 10 you can write multiline anonymous methods with ease

Dim mySub = Sub(newStudent As Student)
Dim myStudent As New Student With {
.Age = 20,
.Name = "Abhishek"
}
newStudent = myStudent
End Sub

Implicitly typed or inferred local variables (Anonymous types): If you have worked in C#, it's exactly how 'var' is used in C#. The object type is inferred from the value it has been assigned. for e.g.

Dim myInt = 4
Dim myString = "Abhishek"
Dim myStudent = New Student With {.Age = 20, .Name = "Abhishek"}

Here how compiler is going to read it
Dim myInt As integer = 4
Dim myString As string= "Abhishek"
Dim myStudent As Student= New Student With {.Age = 20, .Name = "Abhishek"}

In C# you cannot assign a null value to anonymous type ('var') but if the type of object is already inferred, you can assign a null value to it for e.g.
in C#
var myObj = null //not possible
*********************
var myStudent = New Student
myStudent = null //works

in VB.Net
Dim myObj = nothing 'works
myObj will be inferred as 'object' type


There are few more new features which I would love to mention in my next couple of posts for e.g. LinqToXML, QueryComprehension, Extension Methods etc.

Happy reading!!!