Visual Basic .NET Language String interpolation


This new feature makes the string concatenation more readable. This syntax will be compiled to its equivalent String.Format call.

Without string interpolation:

String.Format("Hello, {0}", name)

With string interpolation:

$"Hello, {name}"

The two lines are equivalent and both get compiled to a call to String.Format.

As in String.Format, the brackets can contain any single expression (call to a method, property, a null coalescing operator et cetera).

String Interpolation is the preferred method over String.Format because it prevents some runtime errors from occurring. Consider the following String.Format line:

String.Format("The number of people is {0}/{1}", numPeople)

This will compile, but will cause a runtime error as the compiler does not check that the number of arguments match the placeholders.