简体   繁体   English

为什么IEqualityComparer <T> 等于方法有两个参数?

[英]Why does IEqualityComparer<T> Equals method take two arguments?

I don't understand why does the Equals method take (T x, T y). 我不明白为什么Equals方法需要(T x,T y)。 Don't you already have one instance to compare to when you invoke the method on an instance? 在实例上调用该方法时,您是否已经没有一个实例可以与之进行比较? Same for GetHashCode - why does it need an object instance as a parameter if I'm already invoking it on an existing instance? 与GetHashCode相同-如果我已经在现有实例上调用对象实例,为什么它需要一个对象实例作为参数?

What would happen if I invoke a.GetHashCode(b)? 如果我调用a.GetHashCode(b)会发生什么? Which one is used? 使用哪一个?

The IEqualityComparer<T> interface is for a comparer object. IEqualityComparer<T>接口用于比较器对象。 So, no...you would not already have one instance as part of the implementation. 因此,不...您将没有一个实例作为实现的一部分。

You may be thinking of IEquatable<T> , where it's implemented by the type of object being compared. 您可能会想到IEquatable<T> ,它是由要比较的对象的类型实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IEqualityComparer中令人费解的行为 <T> ,未命中Equals()方法中的断点 - Puzzling behaviour in IEqualityComparer<T>, breakpoint in the Equals() method is not hit 为什么IEqualityComparer <T> 有GetHashCode()方法? - Why IEqualityComparer<T> has GetHashCode() method? IEqualityComparer <T> 不适用于List <T> .Distinct()方法 - IEqualityComparer<T> does not work with List<T>.Distinct() method 我应该如何实现IEqualityComparer <T> 。等于 - How should I implement IEqualityComparer<T>.Equals 为什么我不能在IEqualityComparer中使用customer.Name.contains(“ smith”) <Customer> 等于法 - Why can I not use customer.Name.contains(“smith”) in IEqualityComparer<Customer> Equals method 为什么Linq不提供lambda参数而不是IEqualityComparer <T> ? - Why does Linq not provide a lambda parameter instead of IEqualityComparer<T>? 的IEqualityComparer <T> 不适用于&#39;Contains&#39;方法 - IEqualityComparer<T> not working for 'Contains' method IEqualityComparer为什么不 <T> 在.NET中扩展IEqualityComparer - Why doesn't IEqualityComparer<T> extend IEqualityComparer in .NET 如何使用IEqualityComparer <T> .Equals()在ToLookUp中 <T> () 延期 - How to use IEqualityComparer<T>.Equals() in ToLookUp<T>() Extension 为什么我的动态IEqualityComparer不起作用? - Why does my dynamic IEqualityComparer not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM