They Used String to append , here each time when you add something , a new address is being allocated in the memory.
Whereas in String Builder same address is gets modified.
//INCORRECT
List values = new List(){"This ","is ","Sparta ","!"};
string outputValue = string.Empty;
foreach (var value in values)
{
outputValue += value;
}
//CORRECT
StringBuilder outputValueBuilder =
new
StringBuilder();
foreach
(var value
in
values)
{
outputValueBuilder.Append(value);
}
No comments:
Post a Comment