ZelalemN
5/30/2014 - 4:46 AM

Concatenating using StringBuilder object, much better than using += for string concatnation

Concatenating using StringBuilder object, much better than using += for string concatnation

// This example concatenates using StringBuilder object then writes contents of the 
// StringBuilder object out to a file using streamwriter
 
StringBuilder textSB = new StringBuilder();
string newLine;
 
for(int i = 0; i < 50; i++)
{
	newLine = "Line # " + i;
	textSB.Append(newLine).Append("|").Append("\r\n");
}
 
string filePath = @"c:\output.txt";
using (StreamWriter outFile = new StreamWriter(filePath))
    outFile.Write(textSB.ToString());