Microsoft has made it very clear that as time goes on, C# and VB will get new language features at more or less the same time. In fact, with the release of .NET 4.0, VB and C# are extreamly close in terms of overall functionaltiy (however, VB is still the sole owner of XML literals- sorry C# programmers).
VB 2010 has three excellent updates. The first update is very minor in scope, but leaves a *huge* impact. Simply put, the dreaded "line continuation character" is no longer needed! Yet it is true, VB programmers can now define a single code statement which spans over multiple lines without littering your code files with _ tokens.
The other two updates have to do with how VB handles lambda expressions. As you might recall, under .NET 3.5, VB's lambda support was extreamly limited. Specifically, you would only use a lambda the arguments were processed by a single code statement, and furthermore, this single statement must supply a return value. Under .NET 4.0, VB lambdas are just as expressive a C#'s. Specifically, you can now create multi-statement lambdas, as well as lambdas which represet Subs.
To illustrate all three of these features working together, consider the following VB code snippet which is downloading an e-book using the WebClient class (in the System.Net namespace) in an async manner.
Code Snippet
- Private Sub btnDownload_Click(ByVal sender As System.Object,
- ByVal e As System.EventArgs) Handles btnDownload.Click
-
- ' Create a WebClient object.
- Dim wc As New WebClient()
-
- ' Handle the following event, which will
- ' store the text of the e-book once downloaded.
- AddHandler wc.DownloadStringCompleted, Sub(s, eArgs)
- theEBook = eArgs.Result
- txtBook.Text = theEBook
- End Sub
-
- ' The Project Gutenberg EBook of A Tale of Two Cities, by Charles Dickens.
- ' Download this book in the background on a secondary thread.
- wc.DownloadStringAsync(New
- Uri("http://www.gutenberg.org/files/98/98-8.txt"))
- End Sub
Here, we are handling the DownloadStringCompleted event using the AddHandler statement. However, notice that we have a multi-line lambda, with no return value! Even better, notice how we have no trace of the dreaded underbar.
30a23028-a6a6-4416-b3c4-85ff5c761326|1|5.0