string.concat

  • Thread starter Thread starter Doug Stiers
  • Start date Start date
D

Doug Stiers

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
 
* "Doug Stiers said:
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>
 
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)".
 
Doug Stiers said:
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.
 
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/default.asp?url=/library/en-us/dndotnet/html/fastmanagedcode.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vbnstrcatn.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchperfopt.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp

Hope this helps
Jay
 
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... :)
 
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
 
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
 
Back
Top