difference between + and & operator

  • Thread starter Thread starter Anoj
  • Start date Start date
A

Anoj

Hi All,


is there any performance difference between + and & operator
while concating string litrels.

which one is better and why??


Thanx

Regards,
Anoj Kumar
 
:
: Hi All,
:
:
: is there any performance difference between + and &
: operator while concating string litrels.
:
: which one is better and why??
:
:
: Thanx
:
: Regards,
: Anoj Kumar


I'm not sure if there is a performance difference or not (probably not)
but + is ambiguous as it can be used to concatenate strings or add
numbers. Consider:

console.writeline(3 + 2)
console.writeline(3 & 2)

The first line will output 5 to the console whereas the second line will
output 32.

Use & for concatentations and + for additions.

Ralf
 
_AnonCoward said:
:
: Hi All,
:
:
: is there any performance difference between + and &
: operator while concating string litrels.
:
: which one is better and why??
:
:
: Thanx
:
: Regards,
: Anoj Kumar


I'm not sure if there is a performance difference or not (probably not)
but + is ambiguous as it can be used to concatenate strings or add
numbers. Consider:

console.writeline(3 + 2)
console.writeline(3 & 2)

The first line will output 5 to the console whereas the second line will
output 32.

Use & for concatentations and + for additions.

Ralf

Following on from Ralf, you need to be careful when concatenating
different data types with + as you can generate curious errors when you
attempt to ADD a string and integer together (for example) without first
casting the integer to a string (ie: Cstr() or other .Net method).
 
Anoj said:
is there any performance difference between + and & operator
while concating string litrels.

When concatenating strings, the '&' operator is recommended.
 
Herfried,
When concatenating strings, the '&' operator is recommended.
I don't agree with you, I would keep it on the answers from Ralf and Andrew

Just to let Anoj know my opinion, although he is probably not interested in
that.

Cor
 
Cor Ligthert said:
Huh?!

| The '&' operator is recommended for string concatenation because it
| is defined exclusively for strings and reduces your chances of
generating
| an unintended conversion.
Exactly that we know all. However in this newsgroup is our expirience in my
opinion more like the answer from Ralf and Andrew.

When you disagree with me about that, you are free. I found it better to
tell my opinion about that because of the asked question.

Non unlikely had Anoj read this on MSDN himself already and asked this here
to get a better opinion.

I hope that I am free to disagree with you about this. You are for me free
to disagree with me.

When you don't remember anymore why that is. That is because that an integer
in a concationation can be threaded as a string with Option Strict Off.

:-)

Cor
 
Cor Ligthert said:
Exactly that we know all. However in this newsgroup is our expirience in my
opinion more like the answer from Ralf and Andrew.

When you disagree with me about that, you are free. I found it better to
tell my opinion about that because of the asked question.

Non unlikely had Anoj read this on MSDN himself already and asked this here
to get a better opinion.

I hope that I am free to disagree with you about this. You are for me free
to disagree with me.

When you don't remember anymore why that is. That is because that an integer
in a concationation can be threaded as a string with Option Strict Off.

Nicely put Cor.

Another good reason for using + over & is that is makes the developer
think about who and what the client may do with their application
forcing them to cast variables properly and stops them getting lazy.
It's just like learning to deal with NULL's from a database. These
things should come out during the testing phase.

Personally. I prefer all my developers to use + over & any day. I'd
rather the application be a little larger and work rather than slim and
full of potential disasters :-)

After all, just because MS thinks its right does not mean it is in
reality :-)
 
Anoj,
In addition to the other comments:

For string *literals* there would be no performance difference as both will
do the concatenating at compile time.

For string *variables* there would be no performance difference as both call
String.Concat to do the work.

//000219: Dim s1, s2, r As String
//000220: r = s1 & s2
IL_0001: ldloc.s s1
IL_0003: ldloc.s s2
IL_0005: call string [mscorlib]System.String::Concat(string,
string)
IL_000a: stloc.s r
//000221: r = s1 + s2
IL_000c: ldloc.s s1
IL_000e: ldloc.s s2
IL_0010: call string [mscorlib]System.String::Concat(string,
string)
IL_0015: stloc.s r

I normally use & as its the String Concatenation Operator, while + is the
Addition Operator. As the others have pointed out, + may attempt to add the
numerics, however Option Strict On will normally identify an implicit
conversion problem...


Hope this helps
Jay

| Hi All,
|
|
| is there any performance difference between + and & operator
| while concating string litrels.
|
| which one is better and why??
|
|
| Thanx
|
| Regards,
| Anoj Kumar
|
 
Andrew D. Newbould said:
Another good reason for using + over & is that is makes the developer
think about who and what the client may do with their application forcing
them to cast variables properly and stops them getting lazy. It's just
like learning to deal with NULL's from a database. These things should
come out during the testing phase.

Personally. I prefer all my developers to use + over & any day. I'd rather
the application be a little larger and work rather than slim and full of
potential disasters :-)

I don't see how using '&' would introduce "potential disasters". Using '&'
will perform the type conversions automatically without a need to explicitly
convert every operand to a string. This will make code shorter, more
self-documenting, easier to edit, understand, and maintain than using '+'
and explicit casts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top