String Concatenation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All:

In VB6, we were encouraged never to use "+" to concatenate strings, due to
issues arisiing from Variants. We wre encouraged to use "&" instead.

Since .NET doesn't allow Variants, in your opinion, is it still good
programming practice to use either:

String.Concat("a", "b")
or
"a" & "b"

Or are we now allowed to use "a" + "b" as well.

What has your experience been?

TIA,
 
Hi Joe,

If you set Option Strict On, you can use safely "+" since the compiler will
spot the wrong operations.

Also, if you are concatenating a lot, use the System.Text.StringBuilder
class instead.
--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
If you set Option Strict On, you can use safely "+" since the compiler
will
spot the wrong operations.
However if that is Off you can not safe use it, therefore go on using that
&, it is probably more documentative for VB programmers as well.

Just my thought,

Cor
 
Joe said:
In VB6, we were encouraged never to use "+" to concatenate strings, due to
issues arisiing from Variants. We wre encouraged to use "&" instead.

Since .NET doesn't allow Variants, in your opinion, is it still good
programming practice to use either:

String.Concat("a", "b")
or
"a" & "b"

The recommendation of using '&' instead of '+' still stands. For matters of
readability and potential compile time optimization I'd chosse the latter of
the two samples you gave above. Note behavior differences with 'Option
Strict On' and 'Option Strict Off'.
 
Hi Cor,

Yes, I also recommend to use "&" to concatenate strings and in the same way
"Option Strict On".

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
Back
Top