Ampersand vs plus for concatenating strings in VB.NET

vb.netString Concatenation

vb.net Problem Overview


In VB.NET, is there any advantage to using & to concatenate strings instead of +?

For example

Dim x as String = "hello" + " there"

vs.

Dim x as String = "hello" & " there"

Yes, I know for a lot of string concatenations I'd want to use StringBuilder, but this is more of a general question.

vb.net Solutions


Solution 1 - vb.net

Microsoft's preference is for VB programmers to use & for strings and not +.

> You can also use the + operator to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead.

Solution 2 - vb.net

I've heard good, strong arguments in favor of both operators. Which argument wins the day depends largely on your situation. The one thing I can say is that you should standardize on one or the other. Code that mixes the two is asking for confusion later.

The two arguments I remember right now for favoring &:

  • If you're not using Option Strict and have two numeric strings, it's easy for the compiler to confuse your meaning of of the + operator with, you know, arithmetic addition
  • If you're updating a lot of older vb6-era code it helps not to have to convert the concatenation operators ( and remember: we want consistency).

And for +:

  • If you have a mixed vb/C# shop, it's nice to only have one concatenation operator. It makes it easier to move code between languages and means just that much less of a context switch for programmers when moving back and forth between languages
  • & is almost unique to VB, while + between strings is understood in many languages to mean concatenation, so you gain a little something in readability.

Solution 3 - vb.net

I prefer using & for string concatenations in VB.NET

One reason for this is to avoid any confusion e.g

MessageBox.Show(1 & 2) ' "12"
MessageBox.Show(1 + 2) ' 3

Solution 4 - vb.net

It's safer to use & since you're making your intention clear to the compiler (I want to concatenate these two values and they should both be converted to strings).

Using + can lead to hard to find bugs if the strings are numerical values, at least if the option strict is off.

For example:

1 + "1" = 2 ' this fails if option strict is on
1 & "1" = 11

Edit: though if you're concatenating a non-string you should probably use some better method anyway.

Solution 5 - vb.net

I suppose it is historical (non .NET Visual Basic uses &, not sure why they introduced the +) and a matter of taste (I prefer & because we concatenate strings, we don't add them...).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondcpView Question on Stackoverflow
Solution 1 - vb.netWalterView Answer on Stackoverflow
Solution 2 - vb.netJoel CoehoornView Answer on Stackoverflow
Solution 3 - vb.netcodingbadgerView Answer on Stackoverflow
Solution 4 - vb.netHans OlssonView Answer on Stackoverflow
Solution 5 - vb.netPhiLhoView Answer on Stackoverflow