StringBuilder
Conventional wisdom says that if we roughly know the final size of the resultant string, then we can allocate an appropriate buffer AOT and save ourselves undue allocations. This is the purpose of the StringBuilder class. It is effectively a mutable (changeable) string-based object that works like a dynamic array. It allocates a block of space, which we can copy future string objects into, and allocates additional space whenever the current size is exceeded. Of course, expanding the buffer should be avoided as much as possible by predicting the maximum size we will need and allocating a sufficiently sized buffer AOT.
When we use StringBuilder, we can retrieve the resultant string object by calling the ToString() method. This ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access