Skip to content

[API Proposal]: Add static methods System.Text.Rune.Parse and System.Text.Rune.TryParse to System.Text.Rune (without IFormatProvider? provider & not Explicit Interface Implementations) #129463

Closed as not planned

Description

@tats-u

Background and motivation

PowerShell/PowerShell#27596

char has the APIs char.Parse and char.TryParse but it's inconsistent that Rune doesn't have the same APIs as char.

They are useful for generating Rune values from strings representing single code points in languages like PowerShell (and VB.NET) where char type (literal expression) isn't particularly common and for creating unit tests where Fine Rune values are created from string literals embedded in InlineData.

API Proposal

namespace System.Text;

public struct Rune {
    public static Rune Parse(string s);
    public static bool TryParse(string? s, out Rune result);
}

While recent C# syntax has made implementation easier, you can imagine how more complex it would be without them:

bool TryParse(string? s, out Rune r)
{
    switch (s)
    {
        case [char c]:
            return Rune.TryCreate(c, out r);
        case [char hi, char lo]:
            return Rune.TryCreate(hi, lo, out r);
        default:
            r = default;
            return false;
    }
}

API Usage

string strToCast = "😀"; // supplementary character (can't be stored in `char`)

if (!Rune.TryParse(strToCast, out Rune r)) {
    Console.Error.WriteLine($"Cast failed from \"{strToCast}\" to Rune in programming language FooBarBaz");
}
return r;
[Theory]
[InlineData("😀", 0x1F600, false)]
[InlineData("A", 0x41, true)]
[InlineData("あ", 0x3042, true)]
[InlineData("𠮷", 0x20BB7, true)]
[InlineData("👨‍👩‍👧‍👦", null, false)]
public void Test_SingleCodePoint(string input, uint? codePoint, bool isLetter)
{
    if (codePoint != null)
    {
        Rune rune = Rune.Parse(input);
        Assert.Equal(codePoint.GetValueOrDefault(), rune.Value);
        Assert.Equal(isLetter, Rune.IsLetter(rune));
    }
    else
    {
        Assert.Throws<FormatException>(() => Rune.Parse(input));
    }
}

Alternative Designs

#60231 takes an extra parameter/argument IFormatProvider? provider, which should be unnecessary.

Risks

Only runtime size increase

Activity

  1. added
    api-suggestion
    on Jun 16, 2026
  2. added
    untriaged
    on Jun 16, 2026
  3. dotnet-policy-service commented on Jun 16, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  4. MihuBot commented on Jun 16, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. MihaZupan commented on Jun 16, 2026

    @MihaZupan
    Member

    You can also use TryGetRuneAt or DecodeFromUtf16 that make your example much simpler.
    E.g.

    bool TryParse(string s, out Rune r) => Rune.TryGetRuneAt(s, 0, out r);
    
    bool TryParse(string s, out Rune r) =>
        Rune.DecodeFromUtf16(s, out r, out int consumed) == OperationStatus.Done &&
        consumed == s.Length;
  6. removed
    untriaged
    on Jun 16, 2026
  7. MihaZupan commented on Jun 16, 2026

    @MihaZupan
    Member

    Note that the proposal is only adding explicitly implemented interface methods, so you wouldn't see them show up as Rune.TryParse

  8. reopened this on Jun 16, 2026
  9. added
    untriaged
    on Jun 16, 2026
  10. tats-u commented on Jun 16, 2026

    @tats-u
    Author

    Oh no, #60231 takes the extra parameter IFormatProvider? provider.

  11. changed the title [-][API Proposal]: Add static methods `System.Text.Rune.Parse` and `System.Text.Rune.TryParse` to `System.Text.Rune`[/-] [+][API Proposal]: Add static methods `System.Text.Rune.Parse` and `System.Text.Rune.TryParse` to `System.Text.Rune` (without `IFormatProvider? provider`)[/+] on Jun 16, 2026
  12. tats-u commented on Jun 16, 2026

    @tats-u
    Author
    bool TryParse(string s, out Rune r) => Rune.TryGetRuneAt(s, 0, out r);

    This returns unexpected results for strings consisting of multiple code points.

    bool TryParse(string s, out Rune r) =>
        Rune.DecodeFromUtf16(s, out r, out int consumed) == OperationStatus.Done &&
        consumed == s.Length;

    This does not make r U+0000 NUL (default) for strings consisting of multiple code points.

    In other words, neither of the examples you provided is production-quality code.

  13. changed the title [-][API Proposal]: Add static methods `System.Text.Rune.Parse` and `System.Text.Rune.TryParse` to `System.Text.Rune` (without `IFormatProvider? provider`)[/-] [+][API Proposal]: Add static methods `System.Text.Rune.Parse` and `System.Text.Rune.TryParse` to `System.Text.Rune` (without `IFormatProvider? provider` & not Explicit Interface Implementations)[/+] on Jun 16, 2026
  14. tannergooding commented on Jun 24, 2026

    @tannergooding
    Member

    We're not interesting in providing such a publicly exposed parse API. The logic and handling required is trivial here and can be rolled in your own helper API if you want something reusable.

    You should use the decode APIs and check the result if you need to handle edges like more data existing.

  15. removed
    untriaged
    on Jun 24, 2026
  16. locked and limited conversation to collaborators on Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions