473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

difference between + and & operator

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
9 1948

"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
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
"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
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
"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
> "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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mattias Campe | last post by:
Hi, Depending on if I get an image or a text of a certain URL, I want to do something different. I don't know in advance whether I'll get an image or a text. This is a URL that returns an...
1
by: learning_C++ | last post by:
Hi, I compiled some code. In the function friend ostream& operator<<(ostream& os, const complex c); I use the later argument complex c and complex& c. I can get the same values and there is no...
4
by: Ganesh Gella | last post by:
Hi All, At some places in code, I see following statements, const XMLCh* some_func() { return &*(*i)->m_Value.begin(); } Is that any different from following syntax ?
2
by: duyifan.nju | last post by:
hello, can someone tell me what is the difference between sort() (in stl) & qsort(stdlib.h) thanks in advance.
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
4
by: Joel | last post by:
Run this method: public void test() { bool b; int i=0; b=false; i=0; b=(b && i++==1);
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
5
by: rajm2019 | last post by:
hi all, i want to know that what is the actual difference b/w the character array & character pointer.then how u will get the addrees of a char array char str="be silent like u" char *p1="be...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.