C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

String and StringBuilder - A Common Mistake


Posted Date: 21 Sep 2007    Resource Type: Articles    Category: .NET Framework
Author: Muhammad Zeeshanuddin KhanMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction


Everyone needs the type String in his daily programming. In addition, most of people do a common mistake to choose between String and StringBuilder.

Wrong Approach



String mySentence = null;
mySentence += "I";
mySentence += " ";
mySentence += "am";
mySentence += " ";
mySentence += "writing";
mySentence += " ";
mySentence += "this";
mySentence += " ";
mySentence += "interesting";
mySentence += " ";
mySentence += "article.";


This is not a good approach to deal with String because String is immutable. Therefore, every time when you concatenate the new value a new instance will be made and it will make a great performance loss if concatenation will be done many times.

Right Approach



System.Text.StringBuilder mySentence = new System.Text.StringBuilder();
mySentence.Append("I");
mySentence.Append(" ");
mySentence.Append("am");
mySentence.Append(" ");
mySentence.Append("writing");
mySentence.Append(" ");
mySentence.Append("this");
mySentence.Append(" ");
mySentence.Append("interesting");
mySentence.Append(" ");
mySentence.Append("article.");


The above code will do the same that the wrong approach did but it will lead to the great Performance boost.

Summary


Both approaches are resulting in same manner. You will not see a big difference in one look. However, remember when your code is serving for many requests the above right approach will definitely give you a performance boost.


So remember when you do not want to concatenate then surely use the String and when you have to deal with multiple concatenations then definitely use StringBuilder.





Responses

Author: Vasudevan Deepak Kumar    21 Sep 2007Member Level: Diamond   Points : 0
I would just like to add this comprehensive discussion thread on the topic:

http://www.thescripts.com/forum/thread103955.html

Deepak


Author: latha reddy    04 Oct 2007Member Level: Gold   Points : 0
nice article


Author: latha reddy    04 Oct 2007Member Level: Gold   Points : 0
nice article


Author: poorni    11 Oct 2007Member Level: Bronze   Points : 0
clearly explained..thanx


Author: Puja Sharma    24 Sep 2008Member Level: Gold   Points : 2
Thanks for the well explaind article.
Using stringbuidler instead of strings really improves the performance.

Varun


Author: Prakash Laxman kharvi    24 Sep 2008Member Level: Silver   Points : 1
Use of string builder is one of the best programming practices that every developer should incorporate.


Author: Venkat    06 Oct 2008Member Level: Silver   Points : 0
hi
Really good 1


Author: Darko    06 Oct 2008Member Level: Bronze   Points : 1
This is nice article,
But mySentence is StringBuilder object, so in order to use it as string u must add mySentence.ToString();


Author: Darko    06 Oct 2008Member Level: Bronze   Points : 1
This is nice article,
But mySentence is StringBuilder object, so in order to use it as string u must add mySentence.ToString();


Author: Roopesh Babu Valluru    14 Oct 2008Member Level: Gold   Points : 1
good exapmle man...many knew this but dont know how to use in real time...i think from here onwards they wil try to...

Thx a lot...


Author: Robin Thomas    18 Apr 2009Member Level: Gold   Points : 1
Check this . a String vs StringBuilder performance test

http://www.robinthomas.in/dotnet/stringbuilder-vs-string-concatenations/


Author: greeny_1984    19 Apr 2009Member Level: Diamond   Points : 0
Hi,

Learned a new thing today.

Thanks for sharing the information with us.



Author: sangeetha    28 May 2009Member Level: Gold   Points : 2
System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

1.In stringBuilder, automatically increase and decrease size of string object whenever add elements and delete elements .Where in string , it is fixed size .
2.StringBuilder can performe a insert(),Delete(),Reverse(),Sort() operations etc and we can create multiple instances .Where as String , It can't perfome .
3.StringBuilder takes enough memory for variable .Where as String can't perform




Author: shital umare    13 Jun 2009Member Level: Silver   Points : 1
String is immutable (readonly)i.e every time new string object will be created for each opertion while StringBuilder is mutable,and give u more operation to perform without creating the new object


regards
-Shital


Author: Prasoon Kumar    15 Jun 2009Member Level: Silver   Points : 2
Hi
Good Article ..But More Explanation could have been

added .

But first of all let us make sure when there is a choice of string builder and string comes.


Choice of String and String Builder becomes essential if there is a lot operation involving string.Else essentially both will result into same kind of performance.


But if there is a lot operation involving string , then we are bound to make a choice among the two.

If more operation involving the same string is string is there then we should use String Builder.







Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Use  .  Stringbuilder  .  String  .  Advantage  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sql Server interviewer topics
Previous Resource: DataBase Connectivity and the Handling
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use