Introduction
Description
In previous articles I explained different types of constructors in c#, difference between overloading and overriding in c#, Delegates in c# with example, difference between datareader, dataset and dataadapter in c#
and many articles relating to interview questions . Now I will explain difference between
string and stringbuilder in c#
using asp.net.
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.
Example
string str = "hi";
// create a new string instance
instead of changing the old one
str += "test";
str += "help";
|
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.
Example
StringBuilder sb = new StringBuilder("");
sb.Append("hi");
sb.Append("test
");
string str = sb.ToString();
|
Differences
String
|
StringBuilder
|
It’s
an immutable
|
It’s
mutable
|
Performance
wise string is slow because every time it will create new instance
|
Performance
wise stringbuilder is high because it will use same instance of object to
perform any action
|
In
string we don’t have append keyword
|
In
StringBuilder we can use append keyword
|
String
belongs to System namespace
|
Stringbuilder
belongs to System.Text namespace
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
26 comments :
One important thing to note is, by default string builder capacity is only 16 characters! so if you keep on appending more than 16 characters, the original string builder object will be discarded and a new one with double the capacity will be generated internally.
If you know before hand at least approximate number of characters you will be storing in the string builder, it would be a performance booster to define the string builder with desired capacity.
e.g., StringBuilder sb = new StringBuilder(7000);
This will create the string builder sb with default capacity 7000 characters, if you append even 1 character more than 7000, a new object with capacity of 14000 characters will be created automatically which could become a performance problem if you are using string builder liberally!!
Hope that helps,
Jack Bhanded
very very neat explanation... This is the regular question in interviews.......
excellent!
Nice explanation everyone should be easily understand
clear explaination
Excellent
Excellent...
Good One
excellent information....that are really helpful for us
Thanks
Very gentle explanation
Thank You
I am fresher. i am not getting the difference exactly for string and stringbuilder by the text example... i want a example program with output means i will get better clarity.. please can anyone explain?
Stopwatch wt = new Stopwatch();
wt.Start();
string s1 = "world";
for (int i = 0; i < 100000; i++)
{
s1 = s1.Insert(i,"hello");
}
wt.Stop();
Console.WriteLine(wt.ElapsedMilliseconds);
StringBuilder sb = new StringBuilder(100000);
wt.Start();
for (int i = 0; i < 100000; i++)
{
sb.Append(i);
}
wt.Stop();
Console.WriteLine(wt.ElapsedMilliseconds);
Console.ReadKey();
can you explain why the stringbuilder instance is takinglonger?
Ya thats true, stringbuilder instance takes long time than string instance.
In response to comment number 12 :
Hello Dear Please Modify your code as per below and check. you will come to know the difference.
Stopwatch wt = new Stopwatch();
wt.Start();
string s1 = "";
for (int i = 0; i < 100000; i++)
{
s1 += "i";
}
wt.Stop();
Console.WriteLine(wt.ElapsedMilliseconds);
Stopwatch wt1 = new Stopwatch();
StringBuilder sb = new StringBuilder(100000);
wt1.Start();
for (int i = 0; i < 100000; i++)
{
sb.Append("i");
}
wt1.Stop();
Console.WriteLine(wt1.ElapsedMilliseconds);
if (s1.Length == sb.Length )
{
Console.WriteLine(s1.Length);
Console.WriteLine(sb.Length);
}
Just take the different instance of Stopwatch as Stopwatch wt1 = new Stopwatch();
Nice one! Simple and superb explanation.
Nice Explanation .Iam Easy to UnderStand Every One
great explanation thank u
Great Solution
As comparing the above code String takes time rather than the StringBuilder
Easy way of understanding!! thanks..
grate job, it helpfull to fresher
very nice article
very nice explanation
good explanation...
Tnx for ur writings!!
Note: Only a member of this blog may post a comment.