What is the difference between the "+" and "&" operators when concatenating strings?

Asked

Viewed 716 times

5

In VB.NET, there are two operators I use in concatenating strings, the & and the + (I know there are more efficient methods for concatenating strings, but in this question, I pay attention only to these two operators).

Dim Str1 As String = "Stack"
Dim Str2 As String = "overflow"

Console.WriteLine(Str1 & Str2)
Console.WriteLine(Str1 + Str2)

See working on .NET Fiddle or in the Ideone.

The return of both was Stackoverflow, no practical difference, in my view.

In the following context, what would be the difference of using the & and the +? And what are the names of these operators? In which case should I use each of them? There are differences in readability or performance?

  • I believe that the + be just a "shortcut" to concatenation or just "another way of doing", since it is an arithmetic operator, and the & logical operator, for joining, for the little that I found in google has a small impact on performance too, but this relation is more for when using methods like concat() where a number of procedures are to be carried out, the + is an arithmetic operator for addition even, while the & logical operator "and", in a sentence where "A and B must be equal and of the same type as C" A && B === C

  • @Hebertdelima is VB.Net so none of this is worth it.

1 answer

3


Essentially there is no difference. The & is the VB way of concatenating (it is the legacy style of the language when it was only VB). The + is the VB.NET way of doing it (it was added to the syntax to get closer to C#, its co-sister). So it is taste, it is only recommended to choose one and always use like this. The two live well, as far as possible.

Who is used to C#, and it is common to have to deal with C#, even if it is in code examples for . NET that has much more for C# than VB.NET, the + seems more natural. Some people don’t like it because it can be mistaken for an arithmetic operator. Which even makes some sense in VB which is dynamically typed.

In fact there may be some ambiguity for the compiler when using the +, so it is customary to recommend the &. So the ideal is that you’re always using Option Strict On to be happy.

Behold the confusion in ideone. And in the .NET Fiddle. Also put on the Github for future reference.

In my conception the & is wrong even using the strict type conversion option since concatenation does the conversion. For me it’s one more reason not to use it. But there is taste for everything. If you find it comfortable, it is ok to use, just understand the differences.

Behold a little better at ideone. And in the .NET Fiddle. Also put on the Github for future reference. (Strict On)

Of course, this makes little difference if you consider that the language was not created for robust systems. VB is one more of those languages that started to do scripts. When the era of robust languages for complex systems arose, it was adapting to meet this new demand. But some vices remained. There is no problem in adopting language. But you have to know that you have to live with these things.

For some people one is more intuitive than another. The fact is that if one does not have a basis of understanding of what concatenation is string both are freaks. If she knows how it works it doesn’t matter much which operator is used. & is only used in VB.NET. O + is more or less standard in most languages.

Normal use has no difference in performance. There may be some difference if one of them forces some type conversion. But there you are comparing different things. The comparison is only fair for the same input and same output.

As far as I know they don’t have specific names. Generic is concatenation operator. The symbols have names, but no context with that question.

  • 3

    Actually there is a difference. & is unique to concatenation, while + has the main purpose of adding numbers, and can also convert types followed by concatenation if one of the operands is string. The & continues to be the concatenation pattern even in VB.Net, since the other has a more complex function when it comes to concatenation and can result in runtime errors depending on the type and value of the operands. Microsoft documentation recommends using & for concatenation.

  • 1

    The question is about concatenation of strings. If it was about various operations I would have talked about it. Anyway I put in the example what happens when using the operator + numerical. That’s why I prefer to use Option Strict On and +, it gives build error and does not let do nonsense. Using & produces logical errors that are the worst because they can go unnoticed.

  • @bigown If you do not abuse your good will, I think it was not very clear to me the difference of VB and VB.NET, cited in the first paragraph. Otherwise, everything is fine, as always. Thank you!

  • 1

    @Viníciusbrazil see if it became clearer. In the background the ideal is that only one of them had remained. If it was added because they saw that it would have some benefit, perhaps they realized that they could give more robustness to the code by giving a new semantics in cases that need conversion. But they could not change the semantics of the existing operator. They created a new.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.