+ 2

What is the benefit of StringBuilder in C# ?

What is the problem that concatenation caused ? And how does StringBuilder solve it ?

20th Jun 2021, 7:44 PM
Issam Abdoh
Issam Abdoh - avatar
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".
20th Jun 2021, 8:20 PM
Eymerich
+ 1
is faster too use it when need to manage and operate long strings
21st Jun 2021, 11:35 AM
Kiwwi#
Kiwwi# - avatar