FormattableSb 2.0.22

dotnet add package FormattableSb --version 2.0.22
                    
NuGet\Install-Package FormattableSb -Version 2.0.22
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FormattableSb" Version="2.0.22" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FormattableSb" Version="2.0.22" />
                    
Directory.Packages.props
<PackageReference Include="FormattableSb" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FormattableSb --version 2.0.22
                    
#r "nuget: FormattableSb, 2.0.22"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FormattableSb@2.0.22
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FormattableSb&version=2.0.22
                    
Install as a Cake Addin
#tool nuget:?package=FormattableSb&version=2.0.22
                    
Install as a Cake Tool

FormattableSb

A mutable FormattableString class:

List<DateTime> summerDates = GetSummerDates();

FormattableStringBuilder sqlBuilder = new FormattableStringBuilder()
    .AppendInterpolated($"INSERT INTO dbo.VacationDates (Date)")
    .AppendLine()
    .AppendInterpolated($"VALUES ");

for (int i = 0; i < summerDates.Count; i++)
{
    if (i > 0)
    {
        sqlBuilder
            .AppendInterpolated($",")
            .AppendLine();
    }

    sqlBuilder.AppendInterpolated($"({summerDates[i]})");
}

FormattableString sql = sqlBuilder.ToFormattableString();
// sql.Format:
// INSERT INTO dbo.VacationDates (Date)
// VALUES ({0}),
// ({1}),
// ({2}),
// ...

// sql.GetArguments():
// [
//   System.DateTime,
//   System.DateTime,
//   System.DateTime,
//   ...
// ]
static List<DateTime> GetSummerDates()
{
    DateTime startDate = new(2040, 6, 20);
    DateTime endDate = new(2040, 9, 22);

    List<DateTime> dates = new();

    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
    {
        dates.Add(date);
    }

    return dates;
}

With EF Core:

using VacationingContext context = new();
int rowsAffected = context.Database.ExecuteSql(sql);
// dbug: 6/19/2040 15:59:59.999 RelationalEventId.CommandExecuting[20100] (Microsoft.EntityFrameworkCore.Database.Command)
//       Executing DbCommand [Parameters=[@p0='?' (DbType = DateTime2), @p1='?' (DbType = DateTime2), @p2='?' (DbType = DateTime2), ...], CommandType='Text', CommandTimeout='30']
//       INSERT INTO dbo.VacationDates (Date)
//       VALUES (@p0),
//       (@p1),
//       (@p2),
//       ....

Features:

  • Adheres to the C# language specification
  • Can be used when you want to modify a FormattableString
  • Preserves alignment and format strings

API:

AppendInterpolated:

/// <summary>
/// Appends the specified interpolated string to the end of the composite format string,
/// replacing its arguments with placeholders and adding them as objects.
/// </summary>
/// <param name="handler">The interpolated string to append, along with the arguments.</param>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public FormattableStringBuilder AppendInterpolated([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedHandler handler)

AppendLine:

/// <summary>
/// Appends the default line terminator to the end of the composite format string.
/// </summary>
/// <returns>A reference to this instance after the append operation has completed.</returns>
public FormattableStringBuilder AppendLine()

ToFormattableString:

/// <summary>
/// Creates a <see cref="FormattableString"/> from this builder.
/// </summary>
/// <returns>The object that represents the composite format string and its arguments.</returns>
public FormattableString ToFormattableString()

Setup:

  • Install FormattableSb via NuGet Package Manager, Package Manager Console or dotnet CLI:
Install-Package FormattableSb
dotnet add package FormattableSb

Credits:

  • Thanks to Stephen Toub for the implementation
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.22 54 8/23/2025
2.0.21 53 8/23/2025
2.0.20 52 8/23/2025
2.0.19 239 11/14/2024
2.0.18 133 11/13/2024
2.0.17 2,945 11/17/2023
2.0.16 235 9/2/2023
2.0.15 186 9/2/2023
2.0.14 249 7/3/2023
2.0.13 234 7/1/2023
2.0.12 203 5/31/2023
2.0.11 255 4/7/2023
2.0.10 294 3/9/2023
2.0.9 282 3/9/2023
2.0.8 292 3/9/2023
2.0.7 393 1/19/2023
2.0.6 363 12/31/2022
2.0.5 370 12/23/2022
2.0.4 367 12/15/2022
2.0.3 386 11/26/2022
2.0.2 386 11/26/2022
2.0.1 454 10/21/2022
2.0.0 454 10/15/2022
1.0.5 445 10/5/2022
1.0.4 447 10/1/2022
1.0.3 470 9/28/2022
1.0.2 475 9/27/2022
1.0.1 493 9/27/2022
1.0.0 467 9/27/2022