C
Chizl
Can someone give me a little more detail on why use StringBuilder vs String?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Chizl said:Can someone give me a little more detail on why use StringBuilder vs
String?
Misbah Arefin said:Since C# strings are immutable, an existing string cannot be modified. So,
if
one tries to change a string either with the concatenation operator (+) or
with the Insert, PadLeft, PadRight, Replace, or SubString methods, an
entirely new string is created-leaving the original string intact.
Therefore, operations which would alter strings-instead-cause additional
memory to be allocated. Memory is a scarce resource. And, memory
allocations
are expensive in terms of memory and performance. Consequently, sometimes
String class usage should be avoided.
Everything else that was written is correct. StringBuilders should be used
when modifying a string repeatedly.
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.