Enkidu said:
Doug said:
StringBuilder people...StringBuilder!!!
why? StringBuilder is useful when concatenating an /unknown/ quantity
of items (especially in a loop); In examples such as Göran's, this
will be interpreted by the compiler as string.Concat((month %
10).ToString(), ... , (year / 10).ToString()), which is both simpler
and more performant (it pre-allocates the correct size string, then
backfills [mutating]) than StringBuilder. Likewise, the char[] buffer
approach is perfectly performant and simple. Why over-complicate
things?
Don't get me wrong - StringBuilder has its place, but I'm not sure
that this is it...
Sorry....bad joke...someone at work wants us all to change our string
concats to stringbuilders (there should have been a :^D at the end of
it hehe)
Marc and Doug, is there expert opinion (yourselves included of course!) on
when to use String and when to use StringBuilder? I started using
StrinbBuilder for some things then found that I was doing multiple
conversions to manipulate them - SB to S, do something with S, convert
back to SB and vice versa....
The answer is:
It depends <big big grin>
A string, once instantiated, is immutable (cannot be changed). That means
that whenever you do a string manipulation function (substring, etc), you
are actually creating a new string. That being said:
The compiler, in cases such as the one above, can optimize alot of the
instantiations out of the picture that you wouldn't see. This is because,
even though a string is treated as an Object, at runtime it is treated as a
Value class (struct). Therefore, the compiler can actually perform compile
time value optimizations on strings. A simple example:
string MyFunc(int a, int b)
{
string sa = a.ToString();
string sb = b.ToString();
return sa + sb;
}
The compiler is smart enough to convert that into a concatination that is
back filled (basically what happens is an array of strings is created with
all of the "ToString" calls, on FINAL string is created, and the characters
of each string are then filled into the final string).
But....This can only happen with compile time optimizations. There are
certain situations (as mentioned earlier) in which a StringBuilder would be
useful. Probably one of the most useful situations is going to be NOT when
you are concatinating strings, but when you need to conditionally modify
portions of the final string.
Regardless of MSDN's "suggestions" about performance of String.* functions
versus StringBuilder, I usually use the most readable (or convenient, when
it comes to P/Invoke or COM)...To tell you the truth, in the programming
that I do, string building is the LEAST of my performance problems....It's
pointless to try to make up for a .04 millisecond delay in a function that
takes an overall of 10 milliseconds due to memory read alignment problems...
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?