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

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

13 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!

Arun A said...

Nice explanation everyone should be easily understand

Anonymous said...

clear explaination

Anonymous said...

Excellent

Manisha Pant said...

Excellent...

krishnabhrath said...

Good One

ritesh kumar yadav said...

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

Thanks

Anonymous said...

Very gentle explanation
Thank You

Dinesh chinnadurai 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.

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.