This question already has an answer here:
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.
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 &:
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 additionAnd for +:
Micorosoft's preference is for VB progammers to use & for strings, NOT +.
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...).
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:
Edit: though if you're concatenating a non-string you should probably use some better method anyway.
I prefer using
&
for string concatenations in VB.NETOne reason for this is to avoid any confusion e.g