What are some useful Visual Basic language features recently made available?
Posted: (EET/GMT+2)
The Visual Basic language in .NET has continued to get small but useful language features alongside C#. If you keep older VB applications around, it is worth knowing what is available in recent versions.
Here is a short list of some of the handy new features:
- Auto-implemented properties: less boilerplate code for simple properties. See an example below:
Public Property Name As String
...instead of declaring a backing field manually which was quite long.
- The
Usingstatement improvements: you can now use the C#-styleUsingblock more naturally. For example:
Using connection As New SqlConnection(cs)
connection.Open()
' ...
End Using
- The Async/Await pattern: VB supports asynchronous methods with the same keywords as C#.
Public Async Function GetTextAsync() As Task(Of String)
Using client As New HttpClient()
Return Await client.GetStringAsync("https://example.com")
End Using
End Function
- Finally, string interpolation: easier formatting.
Dim user = "John"
Dim message = $"Hello, {user}!"
If your VB projects are still targeting older .NET or language versions, upgrading the project to use these features can make the code shorter and easier to read without changing the logic.