Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 2.82 KB

File metadata and controls

45 lines (34 loc) · 2.82 KB
description title titleSuffix ms.date helpviewer_keywords ms.assetid
Learn more about: Concatenation Operators in Visual Basic
Concatenation Operators in Visual Basic
07/20/2015
& operator [Visual Basic], concatenation
concatenation operators [Visual Basic]
operators [Visual Basic], concatenation
Visual Basic code, operators
+ operator [Visual Basic], concatenation
concatenation operators [Visual Basic]
e59908c3-89e0-41ae-933d-3e8826c16a04

Concatenation Operators in Visual Basic

Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and &. Both carry out the basic concatenation operation, as the following example shows.

Dim x As String = "Mic" & "ro" & "soft"
Dim y As String = "Mic" + "ro" + "soft"
' The preceding statements set both x and y to "Microsoft".

These operators can also concatenate String variables, as the following example shows.

[!code-vbVbVbalrOperators#76]

Differences Between the Two Concatenation Operators

The + Operator has the primary purpose of adding two numbers. However, it can also concatenate numeric operands with string operands. The + operator has a complex set of rules that determine whether to add, concatenate, signal a compiler error, or throw a run-time xref:System.InvalidCastException exception.

The & Operator is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.

Performance: String and StringBuilder

If you do a significant number of manipulations on a string, such as concatenations, deletions, and replacements, your performance might profit from the xref:System.Text.StringBuilder class in the xref:System.Text namespace. It takes an extra instruction to create and initialize a xref:System.Text.StringBuilder object, and another instruction to convert its final value to a String, but you might recover this time because xref:System.Text.StringBuilder can perform faster.

See also