Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand vs plus for concatenating strings in VB.NET [duplicate]

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.

like image 422
dcp Avatar asked Jun 09 '10 13:06

dcp


People also ask

What is the most efficient way to concatenate many strings together?

When concatenating three dynamic string values or less, use traditional string concatenation. When concatenating more than three dynamic string values, use StringBuilder . When building a big string from several string literals, use either the @ string literal or the inline + operator.

What symbol is used for concatenation in VB?

The ampersand symbol is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings.

How do you join strings in VB net concatenation?

String concatenation is when you combine two or more strings into a single string variable. String concatenation is performed with the & symbol. Non-string values will be converted to string when using & . Always use & (ampersand) to perform string concatenation.


1 Answers

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.

like image 130
Walter Avatar answered Sep 24 '22 08:09

Walter