与号与加号用于在 VB.NET 中连接字符串 [英] Ampersand vs plus for concatenating strings in VB.NET

查看:35
本文介绍了与号与加号用于在 VB.NET 中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,使用&代替+来连接字符串有什么好处吗?

In VB.NET, is there any advantage to using & to concatenate strings instead of +?

例如

Dim x as String = "hello" + " there"

对比

Dim x as String = "hello" & " there"

是的,我知道我想使用 StringBuilder 进行很多字符串连接,但这更像是一个一般性问题.

Yes, I know for a lot of string concatenations I'd want to use StringBuilder, but this is more of a general question.

推荐答案

我听说过支持这两个运营商的良好而有力的论据.哪种论点胜出在很大程度上取决于您的情况.我可以说的一件事是你应该标准化一个或另一个.混合这两者的代码会在以后引起混淆.

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 &:

  • 如果您没有使用 Option Strict 并且有两个数字字符串,那么编译器很容易混淆您对 + 运算符的含义,您知道,算术加法
  • 如果您要更新大量较旧的 vb6 时代代码,则不必转换连接运算符会有所帮助(请记住:我们需要一致性).
  • 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).

对于+:

  • 如果您有一个混合的 vb/C# 商店,那么最好只有一个连接运算符.它使在语言之间移动代码变得更容易,并且意味着程序员在语言之间来回移动时更少的上下文切换
  • & 几乎是 VB 独有的,而字符串之间的 + 在许多语言中被理解为表示连接,因此您可以获得一些可读性.
  • 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.NET 中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆