A deep dive into the new `StringBuilder.MoveChunks()` API introduced in .NET 11 preview 5. The method transfers the internal char[] chunks from a source StringBuilder to a new instance and resets the original, avoiding the allocation cost of calling `ToString()`. The post explains the implementation (essentially a move constructor that copies internal fields), compares it to `Clear()`, and explains the primary motivation: enabling Roslyn to accept a `StringBuilder` directly in a proposed `SourceText.From()` overload without risking mutation by other holders of the original reference. The Roslyn API is not yet implemented, partly because source generators must target netstandard2.0, but the pattern is applicable to similar public APIs in other libraries.

8m read timeFrom andrewlock.net
Post cover image
Table of contents
Building text efficiently with StringBuilderUsing the new StringBuilder.MoveChunks() methodLooking at the implementation of MoveChunks()Why do we need MoveChunks ?So when is the new Roslyn API going to be available?Summary
10.4K Impressions