“Visual Basic 14” is the version of Visual Basic that will ship with Visual Studio 2015. In this blog post I’ll talk specifically about the VB language improvements in this release. (Separately, there are a whole host of IDE and project-system improvements as well). There are two overall themes to the language improvements:
(1) Make common coding patterns a little cleaner, with easy-to-grasp syntax
(2) Fix up some irritating corners of the language that you probably already expected to work.
This release will be easier to digest than was Visual Basic 12, with its introduction of async! (The version number of Visual Basic has gone straight from 12 to 14, skipping 13. We did this to keep in line with the version numbering of Visual Studio itself.)
I’ll only talk here about the most important new language features. For a full exhaustive list, look at roslyn.codeplex.com > Documentation > Language Features.
(Note: this post has still “before/after” pictures. You can also read a version of this post with animated pictures. I think the animations make the new features come alive!)
The ?. operator
The new ?. operator is an easier way to check whether something is null before dotting into it. It’s useful because production-quality code is typically littered with hundreds of null-checks all over the place, and ?. will make them all easier to read. Here’s how you might change your code to use the ?. operator:
One neat trick is to combine it with the null-coalescing (binary) “If” operator, in this case to get a default value:
You can also use ?. to avoid invoking a method off a null reference:
When using ?. to access a field/property that’s a value-type (ie. structure) rather than reference-type (i.e. class), and when invoking a function that returns a value-type, then what you get back is a nullable of that value-type:
Actually, the ?. operator is safer than all the above examples suggest. The expression “customer?.Age” actually reads “customer” into a temporary variable, checks if it’s not null, and then fetches the Age property from that temporary. This is thread-safe even if a different thread happens to null out customer after the check but before reading the property.
You can also use “?.” in a sequence and you can mix with the regular “.” operator, e.g. a?.b.c?.d. It reads left-to-right. Any null value in “?.” will just stop the sequence short, and any null value in “.” will raise a NullReferenceException as usual.
Pro tip: Theoretically the ECMA CLI spec allows a memory model where even the temporary isn’t thread-safe; but in practice in every implementation of .NET the temporary is thread-safe.
Pro tip: When you do ?. to access a field of value-type, you get back a nullable of that value type. But what do you think happens when the compiler doesn’t know whether it’s a value-type or not? For instance in “Sub f(Of T)(x As StrongBox(Of T)) : Dim y = x?.Value : End Sub”.
Pro tip: In addition to ?. there are a few more null-conditional operators: indexing “array?(index)”; delegate invocation “delegate?(args)”; dictionary access “dict?!key”. It also works with XML accessors “x?.<p>” and “x?.@attr” and “x?…<p>”.
The NameOf operator
[Note: NameOf isn’t yet in VS2015 Preview. Watch out for it to arrive before VS2015 finally ships.]
Look at use of the new NameOf operator:
The “before” and “after” behavior is exactly the same: NameOf(customer) simply returns the constant string “customer”, and does this at compile-time. What’s great about using NameOf is that it works with IDE features like Rename and Find All References.
Here’s another place where you’ll likely want to use NameOf:
Pro tip: We decided to make NameOf a new reserved keyword in VB. If you have existing code with methods or variables named “NameOf” then they’ll fail to compile once you upgrade to VS2015, or in some pathological cases you’ll actually get a clean compile but different behavior. You’ll have to escape them as “@NameOf”.
Pro tip: Because NameOf is a constant, you can use it in lots of places – even inside attribute arguments.
Pro tip: You can use qualified expressions like NameOf(customer1.Age). And you can also dot off types like NameOf(Customer.Age).
[Note: String Interpolation isn’t yet in VS2015 Preview. Watch out for it to arrive before VS2015 finally ships.]
String interpolation is my favourite feature this release. I know that ?. is more powerful, and nameof() will make my code more robust, but every time I type an interpolated string it gives me a little shiver of excitement! Here’s how it looks:
String interpolation is nothing more than a syntactic shorthand for String.Format. As such, it also allows format specifiers:
Pro tip: It will also be possible to use different cultures, and also just to extract the raw format-string and arguments (e.g. if you wanted to use it for SQL queries and needed to escape the arguments to prevent string-injection attacks). But we haven’t yet closed on the design for that.
One question we’ve been pondering is: what color should the braces be? the ones that enclose an expression inside the interpolated string? Here are some options…
What do you think?
You used to have to use cumbersome workarounds to get multiline strings in VB. Thankfully VB14 now supports multiline strings literals directly:
One common workaround that people used was to embed their multiline strings into XML literals. Again, this is no longer needed:
Pro tip: VB string literals are now exactly like C# verbatim strings.
Pro tip: Does the multiline string literal use vbCrLf, vbCr, or vbLf for the linebreaks? Answer: it uses whatever line-terminators that your source code file used.
We’ve made it considerably easier to write readonly auto-properties. Here’s how you do it:
As you can see, readonly autoprops can be initialized in-line or in the constructor, just like normal properties. It’s not allowed to assign to a readonly autoprop after the constructor has finished.
Comments are now handled better in statements that split over multiple lines. This is particularly nice for LINQ expressions. Look at these “before” and “after” videos… previously it was simply an error to include these comments:
There are host of smaller features also added to Visual Basic 14. Full details are at roslyn.codeplex.com > Documentation > Language Features.
Another exciting change is that the Visual Basic compiler is now open-source, and the language design process too has moved into the open: the VB team at Microsoft are stewards of the language; we discuss the proposals and comments made by the public and we publish our Language Design Meeting notes publicly. A lot of the new features on this page have been improved by the public. If you have time to contribute to Visual Basic 15, we’d love to listen to your thoughts…
- Download Visual Studio 2015 Preview to try it out
- Visual Studio Uservoice – this is where to raise and vote for your feature requests
- Roslyn.codeplex.com > Language Features – this is where to find the current state of language design
- Roslyn.codeplex.com > Discussions – this is where to comment in-depth on existing language feature proposals, to contribute with your own use-cases that you’d like helped by new language features, and to submit your own proposals.
Lucian Wischik is the Program Manager for Visual Basic. He runs the design meetings and maintains the language specification. He’s also part of the C# language design process.