+ 2
What is the benefit of StringBuilder in C# ?
What is the problem that concatenation caused ? And how does StringBuilder solve it ?
2 Respuestas
+ 5
Strings are immutable objects, so when you do something like this:
var text = "Hello!";
for(var i = 0; i<10;i++){
text += " Hello!";
Console.WriteLine(text);
}
Whenever you add the new string to the text, you are creating a new string that is the sum of the previous, the old string remains in memory waiting the Garbage Collection.
In this way your are wasting memory, StringBuilder solve this issue creating a ”mutable string".
+ 1
is faster too
use it when need to manage and operate long strings