Table of Contents

Getting started

The following section will show you how to use the ValueStringBuilder.

The library targets net8.0, net9.0, and net10.0. Install the nuget-package:

PM> Install-Package LinkDotNet.StringBuilder

Now that the package is installed the library can be used:

using System;

using LinkDotNet.StringBuilder; // Namespace of the library

public static class Program
{
    public static void Main()
    {
        using var stringBuilder = new ValueStringBuilder();

        stringBuilder.AppendLine("Hello World!");
        stringBuilder.Append(0.3f);
        stringBuilder.Insert(6, "dear ");
        Console.WriteLine(stringBuilder.ToString());
    }
} 

Prints: Here is an interactive example where you can fiddle around with the library. The example is hosted on https://dotnetfiddle.net/ and already has the ValueStringBuilder nuget package included in the latest version.

If you are new to the library, these defaults are usually the right choice:

  • start with using var stringBuilder = new ValueStringBuilder();
  • call ToString() only when you actually need a string
  • pass the builder by ref to helper methods
  • reach for stackalloc only after you know the output is small and bounded

For the common pitfalls and the more advanced performance-oriented guidance, see Best practices and pitfalls.

Helper methods

There are also very easy-to-use helper methods, which doesn't need a ValueStringBuilder instance:

using LinkDotNet.StringBuilder;

string helloWorld = ValueStringBuilder.Concat("Hello World!", 101);