469,291 Members | 1,765 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

string.concat

Is there a downside to using string.concat? Other than a little overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do this
functionality.
Thanks in advance.

DougS
Nov 20 '05 #1
8 3356
* "Doug Stiers" <st****@email.com> scripsit:
Is there a downside to using string.concat? Other than a little overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do this
functionality.


Compile a simple project with both versions and have a look at it using
the "ildasm.exe" framework tool.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #2
In article <O1**************@tk2msftngp13.phx.gbl>, st****@email.com
says...
Is there a downside to using string.concat? Other than a little overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2


The code "str1 &= str2" actually compiles into "str1 = String.Concat
(str1, str2)".

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #3
"Doug Stiers" <st****@email.com> schrieb
Is there a downside to using string.concat? Other than a little
overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do
this functionality.
Thanks in advance.


In addition to the other replies:
If you need to concat several strings, you can use a
System.Text.Stringbuilder object.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Doug,
The downside of using String.Concat is its more characters to type! ;-)

As the others have pointed out, and demonstrated how to find out for
yourself. The concatenation operator "&" in VB.NET simple calls the
String.Concat function, so there is no difference execution wise.

However I tend to feel write your programs in the most straight forward
manor first.

At a later time, when one of your routines is proven to have a performance
issue, then you should look at optimizing that routine.

In other words use either "str1 &= str2" or "str1 =
string.concat(str1,str2)" that you and your team are more comfortable with,
trying to be consistent within your project, team, company. When you find
that specific routine has a performance problem, then worry about using the
one over the other or using StringBuilder instead.
The following articles provide information on writing .NET code that
performs well.

http://msdn.microsoft.com/library/de...anagedcode.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

http://msdn.microsoft.com/library/de...vbnstrcatn.asp

http://msdn.microsoft.com/library/de...tchperfopt.asp

http://msdn.microsoft.com/library/de...tperftechs.asp

Hope this helps
Jay

"Doug Stiers" <st****@email.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
Is there a downside to using string.concat? Other than a little overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do this
functionality.
Thanks in advance.

DougS

Nov 20 '05 #5
Addendum:

If you make a lot of concatenations, use the 'StringBuilder' instead of
the string.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #6
In article <O1**************@TK2MSFTNGP12.phx.gbl>, Armin Zingler wrote:
"Doug Stiers" <st****@email.com> schrieb
Is there a downside to using string.concat? Other than a little
overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do
this functionality.
Thanks in advance.


In addition to the other replies:
If you need to concat several strings, you can use a
System.Text.Stringbuilder object.


I would change that to - If you need to concat several strings, you
SHOULD use a System.Text.StringBuilder object... :)
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #7
Cor
Hi Tom,

A little chalenge for you, show us how you can make this faster using
stringbuilder.

It are severall strings.

dim a = "Tom " & "Shelton"

(Only because you use SHOULD)

-))

Cor
Nov 20 '05 #8
String Class in .NET is not optimised at all! Strings are immutable and so
each time you concatenate, you create a new string object somewhere else in
memory. If you do it to many times, there is quite some overhead to it. Use
StringBuilder instead if you need to do concatenation more than a few times.

There should not be any difference between String.Concat and using & or +
sign.

Cheers
Ali
"Doug Stiers" <st****@email.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
Is there a downside to using string.concat? Other than a little overhead?

str1 = string.concat(str1,str2)
vs.
str1 &= str2

It seems to me like the string class should be optimized to do this
functionality.
Thanks in advance.

DougS

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Hector A | last post: by
4 posts views Thread by Ereinion | last post: by
1 post views Thread by Trint Smith | last post: by
3 posts views Thread by Mythran | last post: by
7 posts views Thread by Leonel Gayard | last post: by
27 posts views Thread by user | last post: by
15 posts views Thread by James | last post: by
2 posts views Thread by Tony Johansson | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.