String vs String Builder

Difference between strings of type System.String and
System.Text.StringBuilder

* Strings of type System.String are immutable, where as
Strings of type System.Text.StringBuilder are mutable.
* As Strings of type System.String are immutable performance
could be negatively impacted in applications where we
have intense string manipulations. It is a good practice
to use Strings of type System.Text.StringBuilder in applications
involving intense string manipulations.

eg:
string str = "abcd";
str = str + "1234";
when concatenating str + "1234" then the memory for the
old str variable will be dereferenced and new memory
will allocated.This repeats whenever we are to manipulating
str variable which decreases the performance of the application.

Instead,

StringBuilder str = "abcd";
str.append("1234");
this does the same but it will not dereference the old memory
and will append the new string which will improve the performance
of the application.


No comments: