可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have seen several programmers use &
and +
for string manipulation.
Such as:
dim firstvar as string
dim secondvar as string
dim thirdvar as string
thirdvar = firstvar & secondvar
Or is it:
thirdvar = firstvar + secondvar
Does it matter? If so, why?
回答1:
The +
and &
operators are not identical in VB.NET.
Using the &
operator indicates your intention to concatenate strings, while the +
operator indicates your intention to add numbers. Using the &
operator will convert both sides of the operation into strings. When you have mixed types (one side of the expression is a string, the other is a number), your usage of the operator will determine the result.
1 + "2" = 3 'This will cause a compiler error if Option Strict is on'
1 & "2" = "12"
1 & 2 = "12"
"text" + 2 'Throws an InvalidCastException since "text" cannot be converted to a Double'
So, my guideline (aside from avoiding mixing types like that) is to use the &
when concatenating strings, just to make sure your intentions are clear to the compiler, and avoid impossible-to-find bugs involving using the +
operator to concatenate.
回答2:
Consider if you are better off using String.Format when concatenating strings. Usually, the code ends up making more sense that way.
Also, if you concatenate many times, consider using a StringBuilder rather than a String.
回答3:
In general, & will always concatenate strings regardless of types of arguments, while + will not. Since there's no drawback in using & over + otherwise, in situations where you clearly desire string concatenations, it is preferable. It also makes the intent of code slightly clearer.
回答4:
The ampersand is the recommended method. The plus operator will work, sometimes, for string concatenation but is not considered correct and will occasionally produce unintended results.
回答5:
They are identical in VB.NET when working with 2 strings. The "&" operator is there for backwards compatibility in VB 6 and earlier, where & was the string concatenation operator, and + did not work for text.
There is a difference if one of your two operands is not a string, as bdukes pointed out. However, in this situation, I highly recommend using String.Format or a StringBuilder (depending on the number/types of operations) to construct the result string from mixed types.
Overall, I would recommend using +, for a single reason. If you do ever decide to translate the code to another language (ie: C#), the + operator will match more with the translated version. It will probably be easier for people coming from another language to understand and follow your code.
回答6:
Regardless of the numerous hints to "stick to one OR the other" - it depends heavily what you combine.
In short, i would use "&" for strings and "+" only for arithmetical operations
Why? Take a look at https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/addition-operator#remarks
All these combinations with toggling the strict-option are like rolling the dice for the compiler... and is never obvious for the reader.
I will also add https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/concatenation-operators#differences-between-the-two-concatenation-operators with the clear statement:
The + Operator has the primary purpose of adding two numbers.