Introduction

edit

Meziantou.Framework.FixedStringBuilder provides value types that hold a string of up to a fixed number of characters: FixedStringBuilder8, FixedStringBuilder16, FixedStringBuilder32 and FixedStringBuilder64. The characters are stored in the struct itself, so building, comparing and formatting the text allocates nothing on the heap until you call ToString():

FixedStringBuilder16 sb = $"Hello {"World"}";
sb.AppendLiteral("!");

string str = sb.ToString();
Assert.Equal("Hello World!", str);

Use them for short texts whose maximum length you know: identifiers, codes, keys, labels, log fields. A value that does not fit throws an ArgumentException: the types never truncate.

Packages

PackageContains
Meziantou.Framework.FixedStringBuilderThe built-in types and the IFixedString interfaces, in the Meziantou.Framework.FixedStringBuilder namespace.
Meziantou.Framework.FixedStringBuilder.GeneratorA source generator that creates a type of any capacity from a partial struct, and its analyzer.

Next steps