Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between String and Stringbuilder in C#, Asp.net

Nov 28, 2013
Introduction

Here I will explain what is difference between string and stringbuilder in c# using asp.net.

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

26 comments :

Anonymous said...

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

Bajid Khan said...

very very neat explanation... This is the regular question in interviews.......

Anonymous said...

excellent!

Unknown said...

Nice explanation everyone should be easily understand

Anonymous said...

clear explaination

Anonymous said...

Excellent

Unknown said...

Excellent...

krishnabhrath said...

Good One

Unknown said...

excellent information....that are really helpful for us

Thanks

Anonymous said...

Very gentle explanation
Thank You

Unknown said...

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?

Anonymous said...

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?

Anonymous said...

Ya thats true, stringbuilder instance takes long time than string instance.

Nitin Gupta said...

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();

Anonymous said...

Nice one! Simple and superb explanation.

Manish Kumar Gautam said...
This comment has been removed by the author.
Unknown said...

Nice Explanation .Iam Easy to UnderStand Every One








Unknown said...

great explanation thank u

Anonymous said...

Great Solution

Chandra Shekhar Paatni said...

As comparing the above code String takes time rather than the StringBuilder

Anonymous said...

Easy way of understanding!! thanks..

Unknown said...

grate job, it helpfull to fresher

Unknown said...

very nice article

AV said...

very nice explanation

Anonymous said...

good explanation...

Unknown said...

Tnx for ur writings!!

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.