By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

difference between + and & operator

P: n/a
Hi All,
is there any performance difference between + and & operator
while concating string litrels.

which one is better and why??
Thanx

Regards,
Anoj Kumar

Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Anoj" <su********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
:
: 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
Nov 21 '05 #2

P: n/a
In message <ld*****************@twister.southeast.rr.com>, _AnonCoward
<ab*@xyz.com> writes

"Anoj" <su********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googleg roups.com...
:
: 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).

--
Andrew D. Newbould E-Mail: ne********@NOSPAMzadsoft.com

ZAD Software Systems Web : www.zadsoft.com
Nov 21 '05 #3

P: n/a
"Anoj" <su********@gmail.com> schrieb:
is there any performance difference between + and & operator
while concating string litrels.


When concatenating strings, the '&' operator is recommended.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
When concatenating strings, the '&' operator is recommended.

I don't agree with you, I would keep it on the answers from Ralf and
Andrew


Huh?!

<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconConcatenationOperators.asp>:

| The '&' operator is recommended for string concatenation because it
| is defined exclusively for strings and reduces your chances of generating
| an unintended conversion.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

P: n/a
> "Cor Ligthert" <no************@planet.nl> schrieb:
When concatenating strings, the '&' operator is recommended.

I don't agree with you, I would keep it on the answers from Ralf and
Andrew


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
Nov 21 '05 #7

P: n/a
In message <u#**************@TK2MSFTNGP09.phx.gbl>, Cor Ligthert
<no************@planet.nl> writes
"Cor Ligthert" <no************@planet.nl> schrieb:
When concatenating strings, the '&' operator is recommended.

I don't agree with you, I would keep it on the answers from Ralf and
Andrew


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.


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

--
Andrew D. Newbould E-Mail: ne********@NOSPAMzadsoft.com

ZAD Software Systems Web : www.zadsoft.com
Nov 21 '05 #8

P: n/a
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

"Anoj" <su********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
| Hi All,
|
|
| is there any performance difference between + and & operator
| while concating string litrels.
|
| which one is better and why??
|
|
| Thanx
|
| Regards,
| Anoj Kumar
|
Nov 21 '05 #9

P: n/a
"Andrew D. Newbould" <ne********@NOzadSPANsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.