Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringbuilder.Append should have an IFormattable overload #55366

Closed
jrmoreno1 opened this issue Jul 8, 2021 · 3 comments
Closed

Stringbuilder.Append should have an IFormattable overload #55366

jrmoreno1 opened this issue Jul 8, 2021 · 3 comments
Labels
untriaged New issue has not been triaged by the area owner

Comments

@jrmoreno1
Copy link

Background and Motivation

StringBuilder has several overloads, but none of them work with an interpolated string without first turning it into a string. Given that purpose of the StringBuilder is performance, it seems like turning the interpolation into a string first is going to give a performance hit.

Proposed API

namespace System.Text
{
     public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
     {
+        public StringBuilder AppendFormat(IFormattable)
     }
}

Usage Examples

const string lastName = "Moreno";
const string firstName = "John";
var sb = new StringBuilder();
IFormattable name = $"{lastName}, {firstName}";
sb.AppendFormat(name);

-->

Risks

It's possible that the implementation for the overload would be slower than sb.AppendFormat("{0}, {1}", lastName, firstName);

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@CyrusNajmabadi CyrusNajmabadi transferred this issue from dotnet/roslyn Jul 8, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jul 8, 2021
@stephentoub
Copy link
Member

This is addressed by #51653. With it, you can just write:

sb.Append($"{lastName}, {firstName}");

and the right things happen.

Closing as a dup of #50635. Thanks.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants