Fundamentals 13 min read

Highlights of Visual Basic 14: Null‑Safety, Interpolation, and Other New Language Features

The article reviews the most notable Visual Basic 14 enhancements—including the null‑conditional operator, CallerNameAttribute, string interpolation, multi‑line strings, readonly auto‑properties, improved namespace resolution, preprocessor directives, and XML documentation support—illustrating each feature with clear VB code examples.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Highlights of Visual Basic 14: Null‑Safety, Interpolation, and Other New Language Features

Visual Basic 14 (VB.NET) jumps from version 12 to 14, introducing many new language features that simplify development and bring parity with C#.

Null support : The null‑conditional operator ?. allows safe navigation of potentially null references. For example:

If customer.PrimaryResidence Is Not Nothing AndAlso customer.PrimaryResidence.Garage IsNot Nothing AndAlso property.PrimaryResidence.Garage.Cars = 2 Then Print("Two Car Garage")

can be reduced to:

If property.PrimaryResidence?.Garage?.Cars = 2 Then Print("Two Car Garage")

The operator can also be combined with If to provide default values:

Dim numberOfCarPorts = If(property.PrimaryResidence?.Garage?.Cars, 0)

Other languages such as Objective‑C support similar null‑handling semantics.

Metaprogramming : VB 12 introduced CallerNameAttribute and the NameOf operator, enabling compile‑time retrieval of member names. Example:

Function Lookup(name As String) As Customer
    If name Is Nothing Then Throw New ArgumentNullException(NameOf(name))
End Function

String interpolation : The $"…" syntax replaces String.Format and reduces counting errors. Example:

Dim message = $"Hello {name}, your amount due is {total:C2}"

When constructing URLs, developers should still encode parameters:

Dim requestUrl = $"http://{server}/customer?name={UrlEncode(customerName)}"

FormattedString object : Interpolated strings may be represented by a FormattedString object that implements IFormattable . Its ToString method forwards to String.Format with a supplied IFormatProvider .

string IFormattable.ToString(string ignored, IFormatProvider formatProvider)
{
    return String.Format(formatProvider, format, args);
}

Multi‑line strings : VB now supports line‑breaks without explicit quotes; the compiler chooses the appropriate line‑separator (vbCrLf, vbCr, vbLf) automatically.

Readonly auto‑properties : Auto‑properties can be declared ReadOnly and initialized either at declaration or in a constructor. The compiler uses copy‑in/copy‑out semantics when such properties are passed by reference.

Public ReadOnly FirstName As String = "Anonymous"
Public ReadOnly LastName As String
Public Sub New(firstName As String, lastName As String)
    Me.FirstName = firstName
    Me.LastName = lastName
End Sub

Comments : Inline comments can now be placed at the end of each line in a multi‑line statement.

Dim emailList =
    From c In Customers
    Where c.IsActive 'ignore inactive customers
    And Not c.DoNotEmail 'we don’t need another spam violation
    Select c.FullName, c.EmailAddress

Structs : Structs now support parameterless constructors, though their execution timing varies depending on how the struct is instantiated.

Data literals : Date literals follow ISO‑8601 format (e.g., #2005-4-3# ) instead of ambiguous US‑style formats.

Interop with C# : The Overrides modifier now implicitly includes Overloads , simplifying cross‑language library usage.

Interface ambiguity : VB disallows ambiguous interface inheritance that C# permits, reducing cases where a call cannot be resolved.

Namespace resolution : VB 14 can resolve ambiguous namespaces such as Threading , offering both System.Threading and System.Windows.Threading in IntelliSense.

TypeOf and IsNot : The IsNot operator can now be used in TypeOf … IsNot … expressions.

If TypeOf sender IsNot Button Then

Preprocessor directives : Two improvements include better region handling and the ability to disable/enable specific warnings.

#Disable Warning BC42356 'suppress warning about no awaits in this method
#Enable Warning BC42356

XML documentation validation : The compiler now validates XML doc comments, checking parameter references and generic type handling in cref tags.

Partial modules and interfaces : Modules and interface declarations can now be marked Partial , aiding code generation and cross‑platform sharing.

(Source: InfoQ)

Null SafetyLanguage featurespreprocessor directivesReadonly Propertiesstring interpolationVB.NET
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.