What is the difference between +
and &
for joining strings in 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With