C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




String and StringBuilder - A Common Mistake


Posted Date: 21 Sep 2007    Resource Type: Articles    Category: .NET Framework

Posted By: Muhammad Zeeshanuddin Khan       Member Level: Bronze
Rating:     Points: 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: Silver   Points : 0
nice article


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


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


Author: varun    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...


Feedbacks      
Popular Tags   What are tags ?   Search 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   BizTalk Adaptors    Web Design

online optimum rewards

Contact Us    Privacy Policy    Terms Of Use