You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Implement ISpanFormattable on StringBuilder #105870
Implementing ISpanFormattable on StringBuilder could reduce string allocations when StringBuilder is being used in interpolated strings & string.Join.
API Proposal
namespaceSystem.Text;publicpartialclassStringBuilder:IFormattable,ISpanFormattable{publicboolTryFormat(Span<char>destination,outintcharsWritten);// Do we also want, to match: ToString(int startIndex, int length)?// public bool TryFormat(Span<char> destination, int startIndex, int length, out int charsWritten);boolISpanFormattable.TryFormat(Span<char>destination,outintcharsWritten,ReadOnlySpan<char>format,IFormatProvider?provider);stringIFormattable.ToString(string?format,IFormatProvider?formatProvider);}
Two added methods are implemented explicitly.
The implementation itself is trival, similar to System.Uri.
Reacted by Alireza Habibi, neon-sunset, Juliano Leal Goncalves, Benjamin Moir, Tim Cassell, Cole Tobin, Huo Yaoyuan, mahdikshk, Paulus Pärssinen, Igor Bagdamyan and 5 more
@Neme12 we get thousands of issues and PRs created each release, things sometimes get lost in the noise. This is especially true with competing prioritization, people moving around, etc.
Then I'd note a thumbs up provides no notification and so it stays on the general backlog. People actually commenting and giving a concrete scenario where it provides benefit is a different story. -- That is not to say people should jump in with "me too" style comments. It is rather stating that people giving a short summary of a concrete scenario they found this would be beneficial can help.
Convenience style APIs are also notably often lower priority. People aren't prevented from using interpolated string handlers + string builder in an allocation free manner, it's just a pain because you can no longer trivially use the friendly language syntax and need to jump through hoop.
Also, I think the public method should not take ReadOnlySpan<char> format, IFormatProvider? provider. Only the explicit interface implementation would need them.
Actually, maybe we don't have to have a public method at all and only explicit interface implementation, because there's already CopyTo, which does the same thing. Though there's no TryCopyTo.
@tannergooding Thanks very much for marking this as ready for review. We should still flesh out the proposal a bit though (public methods vs no public methods etc), but maybe the original author won't see this anymore since it's been years.
The question of public vs not can be solved in API review and I'll ensure its updated appropriately.
It really doesn't really change anything concrete here; just a minor nuance on "is the interface API implicit or explicit" and "if explicit, do we expose the convenient API that hides the unused parameters as is standard"
For TextWriter or any other types, those should be proposals for their own areas as they have distinct area owners and considerations.
I'd like to take this one and will open a PR implementing the approved API, including IUtf8SpanFormattable and the TryFormat overloads with a source range, plus tests for the self-append case (sb.Append($"aa{sb}bb")) mentioned in the proposal.
Activity
dotnet-policy-service commentedon Aug 2, 2024
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
Neme12 commentedon Oct 18, 2026
Why is this issue completely inactive? This is a useful perf addition with lots of upvotes and noone even commented.
tannergooding commentedon Oct 18, 2026
@Neme12 we get thousands of issues and PRs created each release, things sometimes get lost in the noise. This is especially true with competing prioritization, people moving around, etc.
Then I'd note a thumbs up provides no notification and so it stays on the general backlog. People actually commenting and giving a concrete scenario where it provides benefit is a different story. -- That is not to say people should jump in with "me too" style comments. It is rather stating that people giving a short summary of a concrete scenario they found this would be beneficial can help.
Convenience style APIs are also notably often lower priority. People aren't prevented from using interpolated string handlers + string builder in an allocation free manner, it's just a pain because you can no longer trivially use the friendly language syntax and need to jump through hoop.
Neme12 commentedon Oct 18, 2026
TextWritershould implement it as well. I have a use case for that too.Neme12 commentedon Oct 18, 2026
@karakasa Would you please modify the issue and add
TextWriteras well?Neme12 commentedon Oct 18, 2026
Also, I think the public method should not take
ReadOnlySpan<char> format, IFormatProvider? provider. Only the explicit interface implementation would need them.Neme12 commentedon Oct 18, 2026
Actually, maybe we don't have to have a public method at all and only explicit interface implementation, because there's already
CopyTo, which does the same thing. Though there's noTryCopyTo.Neme12 commentedon Oct 18, 2026
@tannergooding Thanks very much for marking this as ready for review. We should still flesh out the proposal a bit though (public methods vs no public methods etc), but maybe the original author won't see this anymore since it's been years.
tannergooding commentedon Oct 19, 2026
The question of public vs not can be solved in API review and I'll ensure its updated appropriately.
It really doesn't really change anything concrete here; just a minor nuance on "is the interface API implicit or explicit" and "if explicit, do we expose the convenient API that hides the unused parameters as is standard"
For
TextWriteror any other types, those should be proposals for their own areas as they have distinct area owners and considerations.bartonjs commentedon Nov 3, 2026
Video
Manish-1kumar commentedon Nov 5, 2026
I'd like to take this one and will open a PR implementing the approved API, including
IUtf8SpanFormattableand theTryFormatoverloads with a source range, plus tests for the self-append case (sb.Append($"aa{sb}bb")) mentioned in the proposal.