New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easy reference equality comparer API #27683
Labels
Milestone
Comments
Is this the API proposal? namespace System.Collections
{
/* NEW proposed type */
public sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
private ReferenceEqualityComparer(); // no access to ctor
// public access to singleton instance
public static ReferenceEqualityComparer Instance { get; }
public bool Equals(object? x, object? y);
public int GetHashCode(object obj); // null argument disallowed
}
} |
@GrabYourPitchforks Yes. |
terrajobst
added
api-approved
API was approved in API review, it can be implemented
and removed
api-ready-for-review
labels
Feb 4, 2020
namespace System.Collections.Generic
{
public sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
private ReferenceEqualityComparer();
public static ReferenceEqualityComparer Instance { get; }
// Plus inherited members from IEqualityComparer, IEqualityComparer<object>
}
} |
@ahsonkhan I will take a look at #31753. Thanks! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Edit by @GrabYourPitchforks on 20 Jan 2020: API proposal is at https://github.com/dotnet/corefx/issues/32931#issuecomment-576453361.
There is an existing internal
ReferenceEqualityComparer
.I would like to be able to do something like this:
Even if I'm not already overriding Equals to consider properties that mutate, using the default equality comparer is dangerous. Future distant changes would silently render this check ineffective. It should be explicitly a reference comparison.
The existing
EqualityComparer<>
class probably shouldn't be used because there's no way to constrainT : class
for theByReference
member. (If C#'s Extension Everything work makes it through, a static extension method would allow putting the generic constraint on the extension method which would only show up onEqualityComparer<T>
for referenced-typedT
.)Not having to specify a type parameter is even nicer. A
ByReference
member wouldn't have to be generic. So long as it returnsIEqualityComparer<object>
, contravariance allows it to just work for all reference types and blocks it for value types:There is no non-generic EqualityComparer class and it could be confusing to introduce one in the
System.Collections.Generic
namespace given the existingComparer
/Comparer<T>
classes. Even so, it does provide the nicest syntax:EqualityComparer.ByReference
.Similar to what the non-generic immutable collection classes do, folks might expect
EqualityComparer.Default<T>()
to exist.Alternatively,
ReferenceEqualityComparer.Instance
would work. It doesn't seem as cool to me asEqualityComparer.ByReference
, but demand for it has obviously been low.If nongeneric IEqualityComparer support is something you'd definitely want from this new API, it would have to return a class type which implements both. Variance is out, so the class would have to be generic and the syntax would have to specify a type parameter.
The text was updated successfully, but these errors were encountered: