Ampersand vs plus for concatenating strings in VB.NET

This question already has an answer here:

  • String manipulation with & or + in VB.NET 6 answers

  • 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).
  • 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.

  • Micorosoft's preference is for VB progammers to use & for strings, NOT +.

    You can also use the + operator to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead.


    I prefer using & for string concatenations in VB.NET

    One reason for this is to avoid any confusion eg

    MessageBox.Show(1 & 2) ' "12"
    MessageBox.Show(1 + 2) ' 3
    
    链接地址: http://www.djcxy.com/p/35134.html

    上一篇: c连接NSString

    下一篇: Ampersand vs plus用于在VB.NET中连接字符串