string vs stringbuilder - 08-12-2005 , 09:43 AM
I'm very new to software field....
Can any1 send me the advantages of Stringbuilder class over String class
Thanks in advance
Re: string vs stringbuilder - 08-12-2005 , 11:28 AM
A good place to take a look at what StringBuilder can do is the overview
page on it in the MSDN library "Using the StringBuilder Class" which can be
The main thing I've found to be the advantage of StringBuilder is it makes
string manipulation less resource intensive.
MCSD (.NET & VS6), ITIL Foundations
This posting is provided "AS IS" with no warranties, and confers no rights.
"H.G.Srivatsa" <Srivatsa.HG (AT) in (DOT) bosch.com> wrote
Re: string vs stringbuilder - 08-13-2005 , 01:24 AM
almost always a better choice
Re: string vs stringbuilder - 08-25-2005 , 08:55 AM
also interned, which means that if two string variables have the same
collection of characters then they are the same string object. This is
more efficient with runtime memory, and it means that the literal
strings take up less space in the application's executable file.
Since String is immutable it means that when you do any string
manipulation (eg ToLower, ToUpper etc) then a new String is created with
the results. Indeed something like this:
string s = "a";
s += "b";
Looks like the string s has the character b added to its end. In fact
this is not the case, another string is created, "ab", and s is assigned
to it. You can imagine that if you do a lot of concatenation then a lot
of string objects are created. These will *eventually* be released by
the garbage collector, but 'eventually' could be some time, during which
your application's memory usage goes up and up.
StringBuilder is designed to allow you to pre-allocate some memory and
then assign characters to it. StringBuilder will monitor the usuage of
the memory buffer and if it needs more it will automatically
re-allocate. Of course, if this happens then the old memory will stay
around until the garbage collector releases it. However, careful
analysis of your code should allow you to determine the amount of memory
you need. Once the you have finished constructing your string you can
tell StringBuilder to create an immutable String from it.
Another aspect (which I won't go in to details, just mention in passing)
is managed/unmanaged code interoperation. If the parameter of an
unmanaged function is a fixed (const) string then you can pass a String.
The interop layer will do conversion to ANSI string if necessary.
However, if you want the unmanaged function to fill a string buffer to
return a string you should not use a String, because it is immutable.
Instead, you can pre-allocate a StringBuilder (define its capacity) and
pass that to the unmanaged function. The interop layer understands
StringBuilder and treats it appropriately. When the unmanaged function
returns you use StringBuilder.ToString() to get access to the string.
my email evpuneqt (AT) zicf (DOT) bet is encrypted with ROT13 (www.rot13.org)
|Thread Tools||Search this Thread|