Skip to content

[API Proposal]: String.EqualsIgnoreCase() #123808

@terrajobst

Description

@terrajobst

Background and motivation

As stated in #14065 invoking string.Equals with the comparison overload feels unintuitive. I believe this comes from two reasons:

  1. Ordinal isn't something people think of when comparing strings. It feels like low-level lingo.
  2. Operands are often compound expressions, which either forces me to assign them to a temporary or live with the fact the operation (Equals) and the way it's being conducted (Ordinal, OrdinalIgnoreCase) is split far part. One place where using a temporary is often not desirable is in lambda expressions where doing so requires converting the expression into a statement block. In case of LinQ this can start to feel really bulky. I have often resorted to extracting helper functions that just do the comparison, but that feels heavy handed too.

Since String.Equals is doing an ordinal comparison anyway, it seems we could address some this by just introducing EqualsIgnoreCase.

API Proposal

namespace System;

public partial class String
{
    public static bool EqualsIgnoreCase(string? a, string? b)
    {
        return Equals(a, b, StringComparison.OrdinalIgnoreCase);
    }
}

API Usage

Before:

var result = docs.Where(d => string.Equals(d.DocumentInfo?.DocumentType, "BusinessReport", StringComparison.OrdinalIgnoreCase);

After:

var result = docs.Where(d => string.EqualsIgnoreCase(d.DocumentInfo?.DocumentType, "BusinessReport");

Alternative Designs

  • String.Equals() already is doing an Ordinal comparison. So just naming it EqualsIgnoreCase rather than EqualsOrdinalIgnoreCase makes more sense.
  • We could decide to include the other enum members of StringComparison as well, but I don't think they are used enough to warrant this, but they could of course be added later.
  • String.Equals isn't intuitive and forces us to do the OrdinalIgnoreCase thing #14065 points out that Ordinal and OrdinalIgnoreCase aren't intuitive terms, which this proposal partially addresses by removing Ordinal.

Risks

None that I can think of.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.RuntimeuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions