-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.RuntimeuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
Description
Background and motivation
As stated in #14065 invoking string.Equals with the comparison overload feels unintuitive. I believe this comes from two reasons:
Ordinalisn't something people think of when comparing strings. It feels like low-level lingo.- 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 itEqualsIgnoreCaserather thanEqualsOrdinalIgnoreCasemakes more sense.- We could decide to include the other enum members of
StringComparisonas 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
OrdinalandOrdinalIgnoreCasearen't intuitive terms, which this proposal partially addresses by removingOrdinal.
Risks
None that I can think of.
distantcam, jonsagara, deadlydog, marcominerva and Mrxx99ladeak
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.RuntimeuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner