What is the difference between strings and string builder in .NET?
-
possible duplicate of http://stackoverflow.com/questions/529999/when-to-use-stringbuilder – Jørn Schou-Rode Apr 01 '10 at 11:20
-
possible duplicate of [String vs. StringBuilder](http://stackoverflow.com/questions/73883/string-vs-stringbuilder) – nawfal Jul 16 '14 at 20:15
4 Answers
A string is an immutable type. It has bad performance characteristics when performing lots of string manipulation like concatenation.
Stringbuilders on the other hand overcome this weakness by keeping a growing buffer so that each concatenation is less likely to require a new string to be allocated.
Since string builders add some overhead, they are only really necessary when some significant string work is to be done (e.g. in a loop). If your code is fast, don't worry about it. If it's not, use a profiler to see if this issue matters in your case.
One final note: this answer really has nothing to do with ASP.NET--this is true of strings in all of .net and lots of other languages, too.

- 105,752
- 40
- 168
- 205
-
1I think your last paragraph is actually more important than all the 'real' answers. – ProfK Jan 04 '10 at 05:36
http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_vs._StringBuilder
Basically, String
s are immutable - every time you manipulate one is needs to be recreated in memory. StringBuilder
is easier on memory and in nearly all cases, much faster when you're dealing with repetitive string concatenation and other manipulative operations.
You may find some better discussion in this SO post: String vs. StringBuilder.
string is immutable and stringbuilder is mutable.
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.
Immutable objects are often useful because some costly operations for copying and comparing can be omitted, simplifying the program code and speeding execution. However, making an object immutable is usually inappropriate if the object contains a large amount of changeable data. Because of this, many languages allow for both immutable and mutable objects.
Each time a concatenation is made to a string object a new string object is created with a new reference and it will be assigned to the object. The older object will still there be in memory.

- 184,426
- 49
- 232
- 263
I would like to answer your question from Javas' perspective though the concept in .Net is the same. In Java we have a package(a package is a collection of classes) known as java.lang, String and StringBuilder are two classes which belong to this package and can be used by other classes to make instances. Now the difference b/w String and String builder is that String objects are immutable meaning the content of these objects cannot be modified as these are constants and the objects of StringBuilder class are mutable meaning their content can be modified.
Ex:-
package anypackagename;
package learningJava;
public class DemoString
{
public static void main(String... args)
{
StringBuilder S2 = new StringBuilder("My name is");
System.out.println("S2 is: "+S2 );
System.out.println("Hashcode of S2 before append "+ S2.hashCode());
String S1 = "I live in";
System.out.println("S1 is: "+S1 );
System.out.println("Hashcode of S1 before concat "+ S1.hashCode());
diff(S1,S2);
}
public static void diff(String S1, StringBuilder S2)
{
System.out.println("\n" );
S2 = S2.append(" Amritansh Upadhyay");
System.out.println("S2 is: "+S2 );
System.out.println("Hashcode of S2 after after " +S2.hashCode());
S1 = S1.concat("Bangalore");
System.out.println("S1 is: "+S1 );
System.out.println("Hashcode of S1 after concat" +S1.hashCode());
}
}
The Hashcode(Value of Ref variable S1) is changed with the appended String but the value of (Value of Ref variable S2) is not changed.

- 135
- 1
- 5