difference between string and stringbuilder in c#


C# | String vs StringBuilder
String:-

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.



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 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 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 same instance of 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# difference between string and stringbuilder in c# Reviewed by NEERAJ SRIVASTAVA on 11:30:00 PM Rating: 5

No comments:

Powered by Blogger.