String concatenation is when you combine two or more strings into a single string variable.
String concatenation is performed with the & symbol.
Dim one As String = "Hello " Dim two As String = "there" Dim result As String = one & two
Non-string values will be converted to string when using &.
Dim result as String = "2" & 10 ' result = "210"
Always use & (ampersand) to perform string concatenation.
DON'T DO THIS
While it is possible, in the simplest of cases, to use the + symbol to do string concatenation, you should never do this. If one side of the plus symbol is not a string, when Option strict is off, the behavior becomes non-intuitive, when Option strict is on it will produce a compiler error. Consider:
Dim value = "2" + 10 ' result = 12 (data type Double) Dim value = "2" + "10" ' result = "210" (data type String) Dim value = "2g" + 10 ' runtime error
The problem here is that if the
+ operator sees any operand that is a numeric type, it will presume that the programmer wanted to perform an arithmetic operation and attempt to cast the other operand to the equivalent numeric type. In cases where the other operand is a string that contains a number (for example, "10"), the string is converted to a number and then arithmetically added to the other operand. If the other operand cannot be converted to a number (for example, "2g"), the operation will crash due to a data conversion error. The
+ operator will only perform string concatenation if both operands are of
& operator, however, is designed for string concatenation and will cast non-string types to strings.