String and StringBuilder
What is the difference between String and StringBuilder .... ??
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Raja TRank #685 · 3.5K reputationPosted Aug 20, 2015, 1:18 AM EDT
Differences between String and StringBuilder:
It belongs to String namespace
It belongs to String. Text namespace
String object is immutable
StringBuilder object is mutable
Assigning:
String s= "something important";
Assigning:
StringBuilder sbuild= new StringBuilder("something important");
We can use '+' operator or Concat method to concatenate the strings.
Here we are using Append method.
When string concatenation happens, additional memory will be allocated.
Here additional memory will be allocated when the string buffer capacity exceeds only.
Raja TRank #685 · 3.5K reputationPosted Aug 20, 2015, 1:21 AM EDT
Strings in C# .NET
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Aug 20, 2015, 2:11 AM EDT
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 doesnt create new space in memory.
Example
StringBuilder sbMyValue = new StringBuilder("");
sbMyValue.Append("Hello Visitor");
sbMyValue.Append("How Are You ??");
string strMyValue = sbMyValue.ToString();
Hemlata KushwahaRank #6,963 · 252 reputationPosted Aug 20, 2015, 2:22 AM EDT
Hemlata KushwahaRank #6,963 · 252 reputationPosted Aug 20, 2015, 2:23 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Aug 20, 2015, 2:26 AM EDT
Raja TRank #685 · 3.5K reputationPosted Aug 20, 2015, 5:20 AM EDT