Understand formatting behavior in Utf8StringInterpolation
mainFormatting in string interpolation follows standard .NET alignment and format specifiers, but the level of support depends on your target runtime:
- .NET 8 and above: Supports all standard .NET formatting because
IUtf8SpanFormattableis implemented. - .NET Standard 2.1, .NET 6, and .NET 7: Uses
Utf8Formatter.TryFormatwithStandardFormat. This is more restrictive and may not be compatible with all standard formats for numeric types. Supported types includebool, byte, Decimal, Double, Guid, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64.
Exception: DateTime, DateTimeOffset, and TimeSpan support regular custom format specifiers across all supported platforms (including .NET Standard 2.1 and .NET 6/7) to overcome the limitations of StandardFormat.
// .NET 8 supports all numeric custom format strings, but .NET Standard 2.1, .NET 6, and .NET 7 do not.
Utf8String.Format($"Double value is {123.456789:.###}");
// DateTime, DateTimeOffset, and TimeSpan support custom format strings on all target platforms.
Utf8String.Format($"Today is {DateTime.Now:yyyy-MM-dd}");