»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.
|