Difference Between String and Stringbuilder in C#

String

String is immutable, Immutable means if you create string object then you cannot modify it and It always create new object of string type in memory.

 Example :-
 string strMyValue = "Hello Visitor";
 // create a new string instance instead of changing the old one
 strMyValue += "How Are";
 strMyValue += "You ??";


Stringbuilder

StringBuilder is mutable, means if create string builder object then you can perform any operation like insert, replace or append without creating new instance for every time.it will update string at one place in memory doesn't create new space in memory.

 Example :-
 StringBuilder sbMyValue = new StringBuilder("");
 sbMyValue.Append("Hello Visitor");
 sbMyValue.Append("How Are You ??");
 string strMyValue = sbMyValue.ToString();

No comments:

Post a Comment