Friday, 27 February 2015

What is difference between string and stringbuilder?

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 objStr = "Sting Value";
// To create a new string instance instead of changing the old.
objStr = objStr + "Value1";
objStr = objStr + "
Value11"; 

String Builder:

                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 objStrBul = new StringBuilder("");
objStrBul .Append("Hello.!!");
objStrBul .Append("HowAreYou? ");
String objStr  = objStrBul.ToString();

No comments:

Post a Comment