27

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ErocM
  • 329
  • 2
  • 6
  • 11
  • 1
    Possible duplicate *[The difference between + and & for joining strings in VB.NET](http://stackoverflow.com/questions/734600/the-difference-between-and-for-joining-strings-in-vb-net)*. – Peter Mortensen Mar 19 '13 at 10:44
  • Does this answer your question? [The difference between + and & for joining strings in VB.NET](https://stackoverflow.com/questions/734600/the-difference-between-and-for-joining-strings-in-vb-net) – Andreas Jul 01 '21 at 13:29

6 Answers6

54

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
  • 152,002
  • 23
  • 148
  • 175
  • Thank you for the reminder. I always forget about that (for reasons I described in my new edit). – Reed Copsey Jul 06 '09 at 17:31
  • Well done bdukes. So I was correct after all, even though I didn't know it, lol. – Bob Mc Jul 06 '09 at 17:56
  • 3
    "+ operator indicates your intention to add numbers" is not true. It generally indicates your intention to add two things together (don't forget about operator overloading!), and will work just fine for 2 strings. The sole advantage of `&` is that it will always result in string concatenation, and will convert all operands to strings. That said, it's still an advantage, so using it is a good idea. – Pavel Minaev Oct 09 '09 at 22:11
  • 2
    Just ran into an issue using `+` instead of `&`. Did something like this: `<%# ResolveUrl("~/page.aspx?id=" + Eval("Id")) %>`. Since ID is an `Integer`, VB tries to convert the left side (the URL) to a numeric type and throws an exception when it can't. – bdukes Nov 20 '09 at 15:15
  • MS's best practices call for & to be used when concatenating strings. Good call bdukes. – Walter Nov 22 '09 at 00:29
5

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
  • 8,738
  • 11
  • 67
  • 129
3

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
  • 99,783
  • 25
  • 219
  • 289
2

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
  • 1,980
  • 1
  • 28
  • 38
  • When will it produce unintended results? Please elaborate to have a complete answer. – jvanderh Jul 06 '09 at 17:00
  • 4
    That's no longer true in VB.NET - VB6 and earlier was this way - vb.net uses the .NET runtime's operator+ on strings, which is supported perfectly. & is an alias for this in VB.NET. – Reed Copsey Jul 06 '09 at 17:00
  • 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 Jul 06 '09 at 17:55
2

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.

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    They are _not_ identical in VB.NET, see my answer for specifics. – bdukes Jul 06 '09 at 17:27
  • 1
    Good point - I always forget about that (although I don't ever see doing 1 & "2" as something I consider appropriate ;) ) – Reed Copsey Jul 06 '09 at 17:29
  • 1
    Edited response to compensate. – Reed Copsey Jul 06 '09 at 17:31
  • 2
    I wouldn't recommend `String.Format()` over anything, much less the dedicated operator `&`, to concatenate strings. I also wouldn't recommend `StringBuilder` when the number of input strings is known in advance, since `String.Concat` (which is what `&` will expand to for chained invocations) is faster, and `&` itself will be clearer. – Pavel Minaev Oct 09 '09 at 22:12
  • @Pavel: You wouldn't use string.format, if one of your operands IS NOT a string? That was what I explicitly suggested. – Reed Copsey Oct 10 '09 at 00:28
0

Regardless of the numerous hints to "stick to one OR the other" - it depends heavily what you combine.

In short, i would use "&" for strings and "+" only for arithmetical operations

Why? Take a look at https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/addition-operator#remarks

All these combinations with toggling the strict-option are like rolling the dice for the compiler... and is never obvious for the reader.

I will also add https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/concatenation-operators#differences-between-the-two-concatenation-operators with the clear statement:

The + Operator has the primary purpose of adding two numbers.

Bernhard
  • 2,541
  • 1
  • 26
  • 24