Open praeceptum opened 8 months ago
Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.
Author: | praeceptum |
---|---|
Assignees: | - |
Labels: | `area-System.Runtime`, `untriaged`, `needs-area-label` |
Milestone: | - |
The implementation of
System.StringComparer
memberint Compare(object? x, object? y)
may not be correct.If the parameters
x
andy
are non-null, non-equal, one isn't typestring
, andx
isn't typeIComparable
, then an exception is raised advising that "At least one object must implement IComparable." However,y
is never tested for, or leveraged as, typeIComparable
. It seems that the behavior and/or the message may be incorrect.For comparison, the
System.Collections.Generic.Comparer<T>
memberint IComparer.Compare(object? x, object? y)
doesn't leverageIComparable
for its parameters. ( It simply raises an exception if they can't be cast toT
. ) Because theStringComparer
implementation does, subtypes that rely on it will compare non-string types. This seems to exceed theIComparer
contract, suppressing semantic errors. For example: