The difference between + and & for joining strings in VB.NET
vb.netStringvb.net Problem Overview
What is the difference between +
and &
for joining strings in VB.NET?
vb.net Solutions
Solution 1 - vb.net
There's no difference if both operands are strings. However, if one operand is a string, and one is a number, then you run into problems, see the code below.
"abc" + "def" = "abcdef"
"abc" & "def" = "abcdef"
"111" + "222" = "111222"
"111" & "222" = "111222"
"111" & 222 = "111222"
"111" + 222 = 333
"abc" + 222 = conversion error
Therefore I recommend to always use &
when you mean to concatenate, because you might be trying to concatenate an integer, float, decimal to a string, which will cause an exception, or at best, not do what you probably want it to do.
Solution 2 - vb.net
The & operator always makes sure that both operands are strings, while the + operator finds the overload that matches the operands.
The expression 1 & 2
gives the value "12", while the expression 1 + 2 gives the value 3.
If both operands are strings, there is no difference in the result.
Solution 3 - vb.net
None.
As you can see below. These two lines of code compiles exactly to the same CIL code:
Module Module1
Sub Main()
Dim s1 As String = "s1"
Dim s2 As String = "s2"
s2 += s1
s1 &= s2
End Sub
End Module
Compiles to (note System.String::Concat
):
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] string s1,
[1] string s2)
IL_0000: nop
IL_0001: ldstr "s1"
IL_0006: stloc.0
IL_0007: ldstr "s2"
IL_000c: stloc.1
IL_000d: ldloc.1
IL_000e: ldloc.0
IL_000f: call string [mscorlib]System.String::Concat(string,
string)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: call string [mscorlib]System.String::Concat(string,
string)
IL_001c: stloc.0
IL_001d: nop
IL_001e: ret
} // end of method Module1::Main
Solution 4 - vb.net
There is no difference in most of the cases. However, the best practice is:
"+" should be reserved for integer additions, because if you don't use Option Strict On then you might have really messed up situations such as:
Input + 12
might give you 20
instead of 812
. This can be especially bad in an ASP.NET application where the input comes from POST/GET.
Simply put: For joining strings, always use "&" instead of "+".
Obviously, use StringBuilder where it's suitable :)
Solution 5 - vb.net
The + operator can be either addition or concatenation. The & is only concatenation. If the expressions are both strings the results would be the same.
I use & when working with strings, and + when working with numbers, so there is never confusion about my intent. If you mistakenly use + and one expression is a string and one is a number, you run the risk of un-desired results.
Solution 6 - vb.net
If both of the types are statically typed to System.String, there is zero difference between the code. Both will resolve down to the String.Concat member (this is what +
does for strings).
However, if the objects are not strongly typed to string, Visual Basic late binding will kick in and go two very different routes. The +
version will attempt to do an add operation which literally tries to add the objects. This will do all manner of attempts to convert both values to a number and then add them.
The &
operator will attempt to concatenate. The Visual Basic runtime will go through all manner of conversions to convert both values to strings. It will then String.Concat
the results.
Solution 7 - vb.net
Straight from MSDN Documentation: Concatenation Operators in Visual Basic
> Differences Between the Two Concatenation Operators > > The + Operator (Visual Basic) has the primary purpose of adding two > numbers. However, it can also concatenate numeric operands with string > operands. The + operator has a complex set of rules that determine > whether to add, concatenate, signal a compiler error, or throw a > run-time InvalidCastException exception. > > The & Operator (Visual Basic) is defined only for String operands, and > it always widens its operands to String, regardless of the setting of > Option Strict. The & operator is recommended for string concatenation > because it is defined exclusively for strings and reduces your chances > of generating an unintended conversion.
Do trust MSDN! :-)
Solution 8 - vb.net
None when joining strings:
Dim string1 As String = "A" + "B"
Dim string2 As String = "A" & "B"
If string1.Equals(string2) And string2.Equals(string1) Then
Debugger.Break()
End If