Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

StringBuilder and Strings and Concantenation in c#

This small article will show you the use of StringBuilder class when concantenating strings




string str = "";

for(int i = 0; i<500; i++)

{

str = str + i.ToString();

}



Here we are defining a string called str and after that in the for loop we are concatenating the old one with the new to get a string. Do you know when we do like that we are assigning it again and again? It is like assigning it 499 times.




That is where the StringBuilder comes in to use. You are defining it only once and add all the strings into it as in the below example.




StringBuilder str = new StringBuilder(10000);

for(int i = 0; i<500; i++)

{

str.Append(i.ToString());

}



If you need a large number of string concantenation StringBuilder is the way to go.

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy