Skip to content

[API Proposal]: Add ToLowerOrdinal() & ToUpperOrdinal() #90999

Closed

Description

@Neme12

Background and motivation

Since #32247/#40910, this code

string.Equals(a, b, StringComparison.OrdinalIgnoreCase)

is no longer equivalent to this code

a.ToUpperInvariant() == b.ToUpperInvariant()

That's why I'm proposing to add ToLowerOrdinal() and ToUpperOrdinal() methods that would be guaranteed to have the same semantics as OrdinalIgnoreCase comparisons, and that would also be guaranteed to be case folded letter-by-letter and have the same length as the input string (and IMO, ToLowerInvariant and ToUpperInvariant should be changed to use proper ICU case semantics that wouldn't have to result in the same length, as per #32247 (comment), but that's a separate discussion).

API Proposal

 namespace System;

 public sealed partial class String
 {
     public string ToLower();
     public string ToLower(CultureInfo? culture);
     public string ToLowerInvariant();
+    public string ToLowerOrdinal();
     public string ToUpper();
     public string ToUpper(CultureInfo? culture);
     public string ToUpperInvariant();
+    public string ToUpperOrdinal();
 }

 public readonly partial struct Char
 {
     public static char ToLower(char c);
     public static char ToLower(char c, CultureInfo culture);
     public static char ToLowerInvariant(char c);
+    public static char ToLowerOrdinal(char c);
     public static char ToUpper(char c);
     public static char ToUpper(char c, CultureInfo culture);
     public static char ToUpperInvariant(char c);
+    public static char ToUpperOrdinal(char c);
 }

 namespace System.Text;

 public readonly partial struct Rune
 {
     public static Rune ToLower(Rune value, CultureInfo culture);
     public static Rune ToLowerInvariant(Rune value);
+    public static Rune ToLowerOrdinal(Rune value);
     public static Rune ToUpper(Rune value, CultureInfo culture);
     public static Rune ToUpperInvariant(Rune value);
+    public static Rune ToUpperOrdinal(Rune value);
 }

 namespace System;

 public static partial class MemoryExtensions
 {
     public static int ToLower(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo? culture);
     public static int ToLowerInvariant(this ReadOnlySpan<char> source, Span<char> destination);
+    public static int ToLowerOrdinal(this ReadOnlySpan<char> source, Span<char> destination);
     public static int ToUpper(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo? culture);
     public static int ToUpperInvariant(this ReadOnlySpan<char> source, Span<char> destination);
+    public static int ToUpperOrdinal(this ReadOnlySpan<char> source, Span<char> destination);
 }

API Usage

...

Alternative Designs

No response

Risks

No response

cc @GrabYourPitchforks

Activity

  1. added
    api-suggestion
    on Aug 23, 2023
  2. ghost added
    needs-area-label
    on Aug 23, 2023
  3. ghost added
    untriaged
    on Aug 23, 2023
  4. changed the title [-][API Proposal]: [/-] [+][API Proposal]: Add `ToLowerOrdinal()` & `ToUpperOrdinal()`[/+] on Aug 23, 2023
  5. added and removed
    needs-area-label
    on Aug 23, 2023
  6. added this to the 9.0.0 milestone on Aug 23, 2023
  7. ghost removed
    untriaged
    on Aug 23, 2023
  8. tarekgh commented on Aug 23, 2023

    @tarekgh
    Member

    This proposal will help resolving #67873 too.

  9. self-assigned this
    on Aug 23, 2023
  10. stephentoub commented on Jun 28, 2024

    @stephentoub
    Member

    @GrabYourPitchforks, is this something we should do now?

  11. tarekgh commented on Jun 29, 2024

    @tarekgh
    Member

    The proposal looks good to me. If we get someone to help with the implementation, we can consider it.

  12. modified the milestones: 9.0.0, 10.0.0 on Jul 22, 2024
  13. 3 remaining items

  14. MichalPetryka commented on Jun 19, 2026

    @MichalPetryka
    Contributor

    This would really help my case-insensitive string IEqualityComparer - doesn't feel safe to use OrdinalIgnoreCase in the string comparison but ToUpperInvariant in the GetHashCode.

    I believe that asking for a StringComparison overload of GetHashCode would be better for that.

  15. Neme12 commented on Jun 20, 2026

    @Neme12
    Author

    The proposal looks good to me. If we get someone to help with the implementation, we can consider it.

    Can it go into API Review then?

  16. modified the milestones: Future, 11.0.0 on Jun 20, 2026
  17. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jun 20, 2026
  18. rampaa commented on Jun 21, 2026

    @rampaa

    I believe that asking for a StringComparison overload of GetHashCode would be better for that.

    If I am not mistaken this already exists. str.GetHashCode(StringComparison.Ordinal) already works today.

  19. bartonjs commented on Jun 23, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed.

     namespace System;
    
     public sealed partial class String
     {
        public string ToLowerOrdinal();
        public string ToUpperOrdinal();
     }
    
     public readonly partial struct Char
     {
        public static char ToLowerOrdinal(char c);
        public static char ToUpperOrdinal(char c);
     }
    
     namespace System.Text;
    
     public readonly partial struct Rune
     {
        public static Rune ToLowerOrdinal(Rune value);
        public static Rune ToUpperOrdinal(Rune value);
     }
    
     namespace System;
    
     public static partial class MemoryExtensions
     {
        public static int ToLowerOrdinal(this ReadOnlySpan<char> source, Span<char> destination);
        public static int ToUpperOrdinal(this ReadOnlySpan<char> source, Span<char> destination);
     }
  20. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 23, 2026
  21. tarekgh commented on Jul 9, 2026

    @tarekgh
    Member

    Should be addressed by #130140.

  22. locked and limited conversation to collaborators on Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Globalization

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions