difference between string and stringbuilder in c#
C# | String vs StringBuilder
String:-
The string is immutable. Immutable means once we create
string object we cannot modify. Any operation like insert, replace or append
happened to change string simply it will discard the old value and it will
create a new instance in memory to hold the new value.
String Builder:-
String builder is mutable it means once we create
string builder object we can perform any operation like insert, replace or
append without creating a new instance for every time.
String
Builder
|
String
|
String
builder is mutable it means once we create string builder object we can
perform any operation like insert, replace or append without creating new
instance for every time.
|
String
is immutable. Immutable means once we create string object we cannot modify.
Any operation like insert, replace or append happened to change string simply
it will discard the old value and it will create new instance in memory to
hold the new value.
|
Recommended
for loops, as only the size of the object will change.
|
Avoid
using inside loops, as if the loop executes N times. N memory allocation
operations will be performed.
|
When
changed, no new object is created or old object is destroyed.
|
When
changed, a new object is created and old object is destroyed.
|
Performance
wise StringBuilder is high because it will use the same instance of the object to
perform any action.
|
Performance
wise string is slow because every time it will create new instance
|
StringBuilder
belongs to System.Text namespace
|
String
belongs to System namespace
|
Concat
function is used for String concatenation.
|
Append
function is used for String concatenation.
|
string
str = "Neeraj";
str = string.Concat(str,
"Srivastava");
|
StringBuilder
sb = new StringBuilder();
sb.Append("Neeraj");
sb.Append("Srivastava");
|
difference between string and stringbuilder in c#
Reviewed by NEERAJ SRIVASTAVA
on
11:30:00 PM
Rating:
No comments: