views:

778

answers:

5

I just had a simple question, but I cannot seem to find a straightforward answer. I've been wondering this for a while, though.

I have seen several programmers use & and + for string manipulation.

Such as:

dim firstvar as string
dim secondvar as string
dim thirdvar as string

thirdvar = firstvar & secondvar

Or is it:

thirdvar = firstvar + secondvar

Does it matter? If so, why?

+1  A: 

The ampersand is the recommended method. The plus operator will work, sometimes, for string concatenation but is not considered correct and will occasionally produce unintended results.

Bob Mc
What unintended results?
Robert Harvey
When will it produce unintended results? Please elaborate to have a complete answer.
jvanderh
Reed Copsey
See my answer below for the unintended result
bdukes
Reed is correct, my lengthy VB6 experience led me to an invalid response. I tested both in VB.Net and both work properly. However, I would still use the ampersand for clarity, as noted by another poster.
Bob Mc
+2  A: 

They are identical in VB.NET when working with 2 strings. The "&" operator is there for backwards compatibility in VB 6 and earlier, where & was the string concatenation operator, and + did not work for text.

There is a difference if one of your two operands is not a string, as bdukes pointed out. However, in this situation, I highly recommend using String.Format or a StringBuilder (depending on the number/types of operations) to construct the result string from mixed types.

Overall, I would recommend using +, for a single reason. If you do ever decide to translate the code to another language (ie: C#), the + operator will match more with the translated version. It will probably be easier for people coming from another language to understand and follow your code.

Reed Copsey
They are _not_ identical in VB.NET, see my answer for specifics.
bdukes
Good point - I always forget about that (although I don't ever see doing 1 ) )
Reed Copsey
Edited response to compensate.
Reed Copsey
Pavel Minaev
@Pavel: You wouldn't use string.format, if one of your operands IS NOT a string? That was what I explicitly suggested.
Reed Copsey
+11  A: 

The + and & operators are not identical in VB.NET.

Using the & operator indicates your intention to concatenate strings, while the + operator indicates your intention to add numbers. Using the & operator will convert both sides of the operation into strings. When you have mixed types (one side of the expression is a string, the other is a number), your usage of the operator will determine the result.

1 + "2" = 3 'This will cause a compiler error if Option Strict is on'
1 & "2" = "12"
1 & 2 = "12"
"text" + 2 'Throws an InvalidCastException since "text" cannot be converted to a Double'

So, my guideline (aside from avoiding mixing types like that) is to use the & when concatenating strings, just to make sure your intentions are clear to the compiler, and avoid impossible-to-find bugs involving using the + operator to concatenate.

bdukes
Thank you for the reminder. I always forget about that (for reasons I described in my new edit).
Reed Copsey
Thank you for clearing that up for me!
ErocM
Well done bdukes. So I was correct after all, even though I didn't know it, lol.
Bob Mc
Pavel Minaev
bdukes
Walter
+2  A: 

In general, & will always concatenate strings regardless of types of arguments, while + will not. Since there's no drawback in using & over + otherwise, in situations where you clearly desire string concatenations, it is preferable. It also makes the intent of code slightly clearer.

Pavel Minaev
+3  A: 

Consider if you are better off using String.Format when concatenating strings. Usually, the code ends up making more sense that way.

Also, if you concatenate many times, consider using a StringBuilder rather than a String.

Patrick Szalapski