Choosing a String Concatenation Method in Visual Basic .NET
Pick &, interpolated strings, or StringBuilder for VB.NET message assembly under Option Strict On, with validation steps.
09 Aug 2025, 20:14 UTC

Problem: You are building user-facing messages in Visual Basic .NET on .NET Framework 4.8 or .NET 6+, and the project requires Option Strict On, a compiler setting that blocks implicit narrowing conversions so a number cannot be silently treated as text. You need a concatenation method that is readable, type-safe, and predictable about memory allocations.
Takeaway: Use the & operator for short messages with a few parts. Use interpolated strings ($"...") when you insert variables into a template. Use StringBuilder inside loops or when appending many items. Avoid + for concatenation unless both operands are already strings.
The decision and constraints
The decision is which operator or type to use for assembling UI strings. Constraints:
- Target runtime: .NET Framework 4.8 or .NET 6+. String concatenation behavior is consistent across these targets; the Visual Basic language version matters more than the runtime.
Option Strict Onmust remain enabled. It prevents implicit conversions, so mixing numbers and strings without an explicit conversion produces a compile error.- Code should remain readable for future maintenance.
- Allocation behavior should be predictable, especially in loops.
Supported options compared
| Option | Best for | Behavior under Option Strict On | Allocation pattern | Important note |
|---|---|---|---|---|
& operator | Short, fixed-part messages | Type-safe; intended for string concatenation | New immutable string per operation | Call .ToString() explicitly on numeric values if the compiler does not accept the operand |
+ operator | Only when both operands are confirmed strings | Can be ambiguous; under Option Strict Off it may add numbers instead of concatenating | New immutable string per operation | Avoid for general concatenation |
StringBuilder.Append | Loops and many appends | Requires Imports System.Text; fully type-safe | Mutable buffer; one allocation until ToString | Not thread-safe; synchronize if shared across threads |
Interpolated string $"..." | Templates with variables | Compile-time checked; available in Visual Basic 14 and later | Similar to concatenation; compiler may optimize | Older projects must use String.Format or & |
Trade-offs by scenario
One-off UI message
For a message such as a button label or a single notification, & is clear and idiomatic. Interpolated strings are equally good when the message contains several variables.
Numeric values
Under Option Strict On, do not rely on silent conversion. Either convert explicitly with .ToString() or use an interpolated string, which formats the expression for you.
Repeated appends in a loop
Because strings are immutable—each concatenation creates a new string object—accumulating results with & in a loop creates many short-lived objects. StringBuilder uses a mutable buffer and defers allocation until you call ToString.
Why not +
The + operator is overloaded for addition and concatenation. With Option Strict Off, "Total: " + 10 + 5 can add the numbers before converting to text, producing Total: 15 instead of Total: 105. Even with Option Strict On, it signals the wrong intent for text assembly.
Implementation with Option Strict On
Enable strict checking at the top of the file:
Option Strict On
Imports System.Text
Example: a single formatted message.
Public Function OrderReady(orderId As Integer, customerName As String) As String
Return $"Order {orderId} is ready for {customerName}."
End Function
Example: explicit conversion with &.
Dim statusCode As Integer = 404
Dim message = "Error code " & statusCode.ToString() & " occurred"
Example: building a list in a loop.
Public Function ItemSummaries(items As IEnumerable(Of String)) As String
Dim sb As New StringBuilder()
sb.AppendLine("Items:")
For Each item In items
sb.Append("- ")
sb.AppendLine(item)
Next
Return sb.ToString()
End Function
If you prefer project-level configuration instead of a file-level statement, set Option Strict to On in the Compile tab of Project Properties, or add this to the project file:
<PropertyGroup>
<OptionStrict>On</OptionStrict>
</PropertyGroup>
Enabling Option Strict on an existing project
Open the project in Visual Studio, go to Project Properties > Compile, and set Option Strict to On. Rebuild. Code that previously relied on implicit conversions will now produce compile errors. Fix each site with an explicit conversion such as .ToString(), CStr, or an interpolated string. Risk: enabling this on a large legacy project can surface many errors at once, so enable it branch-by-branch or file-by-file if the team cannot stop for a full rebuild.
Validation
- Rebuild with
Option Strict Onand confirm that mixing a number with a string fails to compile unless the number is explicitly converted. - Confirm the Visual Basic language version supports interpolated strings. Visual Basic 14.0 or later is required; older projects should fall back to
String.Formator&. - Run a Release-mode loop that appends 10,000 items with
&accumulation and again withStringBuilder.Append, then compare memory allocations or elapsed time. Do not expect identical results;StringBuildershould allocate less for repeated appends.
Practical check: after the change, open a form or page that displays assembled messages and verify the text appears exactly as expected, with numbers rendered correctly and no runtime conversion exceptions.
Limitations
StringBuilderis not thread-safe. Lock access if multiple threads append to the same instance.- Interpolated strings require Visual Basic 14.0 or later.
- The
&operator still creates a new string for every operation, so it is not suitable for large loop-based assembly. +remains risky for concatenation because its behavior depends on operand types and theOption Strictsetting.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.