60

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dcp
  • 54,410
  • 22
  • 144
  • 164
  • 1
    @Tim - Thanks, I searched but somehow didn't find that, thank you for posting it. – dcp Jun 09 '10 at 13:26

5 Answers5

49

Microsoft's preference is for VB programmers to use & for strings and not +.

You can also use the + operator to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead.

Bugs
  • 4,491
  • 9
  • 32
  • 41
Walter
  • 2,540
  • 2
  • 30
  • 38
  • 9
    +1 for using the MSDN docs rather than personal opinion! Both are valid, but only one is offical. :) You may also want to link to the docs on & - http://msdn.microsoft.com/en-us/library/wfx50zyk.aspx – Dan Atkinson Nov 21 '13 at 15:57
48

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 &:

  • If you're not using 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 addition
  • If you're updating a lot of older vb6-era code it helps not to have to convert the concatenation operators ( and remember: we want consistency).

And for +:

  • If you have a mixed vb/C# shop, it's nice to only have one concatenation operator. It makes it easier to move code between languages and means just that much less of a context switch for programmers when moving back and forth between languages
  • & is almost unique to VB, while + between strings is understood in many languages to mean concatenation, so you gain a little something in readability.
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 10
    There is a problem with `&` for string concatenation. From the [documentation](http://msdn.microsoft.com/en-us/library/te2585xw.aspx) "The & operator always widens its operands to String, regardless of the setting of Option Strict". So for example `"Hello " & 2.5` will silently convert the 2.5 to a string using the regional settings (you might get `"2.5"` or `"2,5"`). Fine if that was what you wanted. I would much, much rather be forced to specify it explicitly. – MarkJ Sep 19 '11 at 09:37
  • 15
    @MarkJ Actually VB.NET is on a better footing here than C#, even, because at least you have the choice of using `&`. In C#, string concatenation can *always* perform an implicit `ToString`, and there's no way to turn this off. I can't tell you how much I hate this. Glad to see VB.NET got this one right (shocking!) – Roman Starkov May 07 '13 at 19:58
15

I prefer using & for string concatenations in VB.NET

One reason for this is to avoid any confusion e.g

MessageBox.Show(1 & 2) ' "12"
MessageBox.Show(1 + 2) ' 3
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
codingbadger
  • 42,678
  • 13
  • 95
  • 110
10

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:

1 + "1" = 2 ' this fails if option strict is on
1 & "1" = 11

Edit: though if you're concatenating a non-string you should probably use some better method anyway.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
5

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...).

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • 1
    Good point about the meaning of + being overloaded with arithmetic operators, whereas & usually just means "and". – Jordan Rieger Jul 19 '12 at 18:06
  • We concatenate strings, we don't bitwise-and them. Sadly, neither of these operator choices is free from overloaded meaning. – ESV Nov 12 '13 at 20:35
  • @ESV I don't think Visual Basic (before .NET) had bitwise operators, so there was no overloaded meaning there. But indeed, the argument is void for languages using & for other usages. – PhiLho Nov 13 '13 at 10:13