Skip to content

Add StringBuilder(Char) #20848

Closed
Closed

Description

@iSazonov

Rationale

In PowerShell repo we got a report PowerShell/PowerShell#3457 about

var allErrors = new StringBuilder('\n');

It is expected that allErrors initialized with '\n' char.
Really compiler silently convert the char -> ushort -> int and use StringBuilder(int capacity) constructor.
Sample http://ideone.com/1pPHlZ

Currently we already have method Append(char value) so suggestion is to add new constructor StringBuilder(char value).

Proposed API

public sealed partial class StringBuilder : ISerializable 
{ 
        // Proposed constructor
        public StringBuilder(char value);
}

Activity

  1. karelz commented on Mar 31, 2017

    @karelz
    Member

    @iSazonov it makes sense to me. Can you please format it into a formal API proposal? It is just much easier to read and review in larger group. Thanks!

  2. iSazonov commented on Mar 31, 2017

    @iSazonov
    ContributorAuthor

    @karelz Issue was reformatted.

  3. karelz commented on Mar 31, 2017

    @karelz
    Member

    Thanks! I let area owners to check it. From my perspective it is ready for larger API review.

  4. terrajobst commented on Jul 16, 2019

    @terrajobst
    Contributor

    This is a good point. The same problem would also exist for this piece of code:

    var allErrors = new StringBuilder(' ', 8);

    So I suggest we add these two:

    public partial class StringBuilder 
    { 
        public StringBuilder(char value);
        public StringBuilder(char value, int repeatCount);
    }

    @stephentoub mentioned that the sheer fact of us exposing these APIs might cause more issues, when people multi-target and now end up calling the "wrong" APIs. There is also the meta problem that this is a battle we just can't win; C# has an implicit conversion from char to int, so maybe be should just bite the bullet and accept this as a fact of life.

  5. danmoseley commented on Jul 16, 2019

    @danmoseley
    Contributor

    Ouch, I'm surprised this didn't come up before.

  6. terrajobst commented on Jul 16, 2019

    @terrajobst
    Contributor

    Video

    So after discussion we think it's not worth it. It's been 20 years and this isn't a widespread enough problem.

  7. transferred this issue fromdotnet/corefxon Jan 31, 2020
  8. added this to the 5.0 milestone on Jan 31, 2020
  9. ghost locked as resolved and limited conversation to collaborators on Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions