Proposal: adding Append overloads with format and IFormatProvider to StringBuilder #50674
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Runtime
Milestone
Background and Motivation
StringBuilder is easy to use and powerful api. It is often used to serialize some data - eg. to text file. This data commonly have to be in InvariantCulture and have specific format.
Sadly currently there is no way to append primitive value types like double or DateTime with format and IFormatProvider without unnecessary allocation. You can use AppendFormat but value is object so there is a boxing. You can use Append but there is no format nor IFormatProvider parameter.
All existing Append methods use this method internally:
But there is already another overload of this method that can be used:
I don't known if there is another boxing here because of interface use (ISpanFormattable) on struct by the way.
I think I can make these changes, it shouldn't be hard anyway.
Proposed API
Add the following overloads to StringBuilder:
Usage Examples
Alternative Designs
Alternatively only format and IFormatProvider parameters with default null value can be added to existing Append methods but this would be breaking change probably.
Still overloads for DateTime, DateTimeOffset, TimeSpan and Guid need to be added.
Risks
More code unless using alternative design.
The text was updated successfully, but these errors were encountered: