与号与加号用于在 VB.NET 中连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3006153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ampersand vs plus for concatenating strings in VB.NET
提问by dcp
In VB.NET, is there any advantage to using &
to concatenate strings instead of +
?
在 VB.NET 中,使用&
连接字符串而不是连接字符串有什么优势+
吗?
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.
是的,我知道我想使用很多字符串连接StringBuilder
,但这更像是一个一般性问题。
采纳答案by Joel Coehoorn
I've heard good, strong arguments in favor of both operators. Which argument wins the day depends largely on your situation. The one thing I can say is that you should standardize on one or the other. Code that mixes the two is asking for confusion later.
我听说过支持这两个运营商的良好而有力的论据。哪种论点胜出在很大程度上取决于您的情况。我可以说的一件事是你应该标准化一个或另一个。混合这两者的代码会在以后引起混淆。
The two arguments I remember right now for favoring &
:
我现在记得支持的两个论点&
:
- If you're not using
Option Strict
and have two numeric strings, it's easy for the compiler to confuse your meaning of of the+
operator with, you know, arithmetic addition - If you're updating a lot of older vb6-era code it helps not to have to convert the concatenation operators ( and remember: we want consistency).
- 如果您没有使用
Option Strict
并且有两个数字字符串,那么编译器很容易将您对+
运算符的含义与算术加法混淆 - 如果您要更新许多较旧的 vb6-era 代码,则不必转换连接运算符会有所帮助(请记住:我们需要一致性)。
And for +
:
而对于+
:
- If you have a mixed vb/C# shop, it's nice to only have one concatenation operator. It makes it easier to move code between languages and means just that much less of a context switch for programmers when moving back and forth between languages
&
is almost unique to VB, while+
between strings is understood in many languages to mean concatenation, so you gain a little something in readability.
- 如果您有一个混合的 vb/C# 商店,那么最好只有一个连接运算符。它使在语言之间移动代码变得更容易,并且意味着在语言之间来回移动时程序员的上下文切换要少得多
&
几乎是 VB 独有的,而+
字符串之间在许多语言中被理解为表示连接,因此您在可读性方面有所收获。
回答by Walter
Microsoft's preferenceis for VB programmers to use &
for strings and not +
.
Microsoft 的偏好是让 VB 程序员使用&
字符串而不是+
.
You can also use the + operator to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead.
您还可以使用 + 运算符连接字符串。但是,为了消除歧义,您应该改用 & 运算符。
回答by codingbadger
I prefer using &
for string concatenations in VB.NET
我更喜欢&
在 VB.NET 中用于字符串连接
One reason for this is to avoid any confusion e.g
这样做的一个原因是为了避免任何混淆,例如
MessageBox.Show(1 & 2) ' "12"
MessageBox.Show(1 + 2) ' 3
回答by Hans Olsson
It's safer to use & since you're making your intention clear to the compiler (I want to concatenate these two values and they should both be converted to strings).
使用 & 更安全,因为您向编译器表明了您的意图(我想连接这两个值,它们都应该转换为字符串)。
Using + can lead to hard to find bugs if the strings are numerical values, at least if the option strict is off.
如果字符串是数值,则使用 + 会导致难以发现错误,至少在选项 strict 关闭的情况下。
For example:
例如:
1 + "1" = 2 ' this fails if option strict is on
1 & "1" = 11
Edit: though if you're concatenating a non-string you should probably use some better method anyway.
编辑:虽然如果你连接一个非字符串,你可能应该使用一些更好的方法。
回答by PhiLho
I suppose it is historical (non .NET Visual Basic uses &, not sure why they introduced the +) and a matter of taste (I prefer & because we concatenate strings, we don't add them...).
我想这是历史性的(非 .NET Visual Basic 使用 &,不知道他们为什么引入 +)和品味问题(我更喜欢 & 因为我们连接字符串,我们不添加它们......)。