& string concat operator usasge

M

mp

In vb6 it was recommended to avoid & due to overhead
preferrable to use a class such as Karl Peterson's cStringBuilder instead
Help in vbdotnet says to use & to build strings
I was expecting the string object to have a .Append method or similar but
don't find one
when I use the .Concat (which i was guessing was equivalent) method i get
warning
Access of shared member, constant member, enum member or nested type through
an instance; qualifying expression will not be evaluated.
so is & ok to use? or what is preferred method?
thanks
mark
 
J

J.B. Moreno

mp said:
In vb6 it was recommended to avoid & due to overhead
preferrable to use a class such as Karl Peterson's cStringBuilder instead
Help in vbdotnet says to use & to build strings
I was expecting the string object to have a .Append method or similar but
don't find one
when I use the .Concat (which i was guessing was equivalent) method i get
warning
Access of shared member, constant member, enum member or nested type through
an instance; qualifying expression will not be evaluated.
so is & ok to use? or what is preferred method?

It depends upon how much string building you're doing. If you're doing
a lot, particularly a lot of repetitive string building, then you
should use the StringBuilder class.

If it's not a performance problem, then you could just use the &
operator. Or use the StringBuilder if you think it is more readable.

The reason why you're getting a warning on the Concat method is because
it doesn't operate on the instance --- ie. myString.Concat("hello",
"mp") is the same as String.Concat("hello", "mp"). I haven't done or
recall any performance testing (or looking at the IL to see what it
does under the hood) but I wouldn't use it at all -- if there's a
performance problem then use StringBuilder, if there's not and you
think it's more readable then use the & operator. Concat is just
asking for misunderstanding.
 
A

Armin Zingler

mp said:
In vb6 it was recommended to avoid & due to overhead
preferrable to use a class such as Karl Peterson's cStringBuilder instead

Never heard of it - but that means nothing. Only know it was recommend
to use & instead of +.
Help in vbdotnet says to use & to build strings

Probably instead of the + operator. Where did you read it?
I was expecting the string object to have a .Append method or similar but
don't find one

Strings are "immutable". They can not be changed.
when I use the .Concat (which i was guessing was equivalent) method i get
warning
Access of shared member, constant member, enum member or nested type through
an instance; qualifying expression will not be evaluated.

Usage:
newstring = String.concat("a", "b", "c", 17)

Concat is a shared method. You must specify the class name (String) to
use it.
so is & ok to use? or what is preferred method?

Yes, & is ok if there's not much to concatenate. Otherwise use the
System.Text.Stringbuilder class. If has an .Append method that you were
looking for.


Armin
 
M

mp

Armin Zingler said:
mp schrieb: snip
Yes, & is ok if there's not much to concatenate. Otherwise use the
System.Text.Stringbuilder class. If has an .Append method that you were
looking for.


Armin

Thanks
mark
 
M

mp

snip
-- if there's a
performance problem then use StringBuilder, if there's not and you
think it's more readable then use the & operator. Concat is just
asking for misunderstanding.

Thanks
Mark
 
G

Göran Andersson

J.B. Moreno said:
The reason why you're getting a warning on the Concat method is because
it doesn't operate on the instance --- ie. myString.Concat("hello",
"mp") is the same as String.Concat("hello", "mp"). I haven't done or
recall any performance testing (or looking at the IL to see what it
does under the hood) but I wouldn't use it at all -- if there's a
performance problem then use StringBuilder, if there's not and you
think it's more readable then use the & operator. Concat is just
asking for misunderstanding.

Well, in the end the String.Concat method is the only alternative to
using a StringBuilder. The + and & operators uses the String.Concat
method, so you won't get away from it.
 
J

J.B. Moreno

Göran Andersson said:
Well, in the end the String.Concat method is the only alternative to
using a StringBuilder. The + and & operators uses the String.Concat
method, so you won't get away from it.

It may be the same under the hood, but not what I was referring to.
It's visually confusing, particularly if you use an instance to access
the shared method.

(On a totally separate note, this and other recent discussions really
make me wish it was possible to have instance methods that operated
properly when null)
 

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

Top