Show / Hide Table of Contents

Efficient String Building

Dynamic string building with zero allocation overhead is provided on top of BufferWriterSlim<char>. The type originally developed for working with dynamic buffers. When instantiated as a buffer of characters, it can be naturally used as a string builder. The main difference with StringBuilder is that the BufferWriterSlim<char> allows to use stack-allocated and pooled memory. As a result, it doesn't require reallocations of the internal buffer on the heap. A new string interpolation mechanism introduced in C# 10 is fully supported by BufferWriterSlim<char> data type:

using DotNext.Buffers;

using var writer = new BufferWriterSlim<char>(stackalloc char[128]); // preallocate initial buffer on the stack

int x = 10, y = 20;
writer.Interpolate($"{x} + {y} = {x + y}");
writer.WriteLine();
writer.Write("Hello, world!");
writer.Format(42, "X");

Span<char> writtenSpan = writer.WrittenSpan;
string result = writer.ToString();

BufferWriterSlim<char> is ref-like struct so it's not suitable for async scenarios. However, it's possible to use IBufferWriter<char> as a dynamic buffer of characters for building strings. Read this article to find a workaround.

  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾