为什么我们需要 IEqualityComparer,IEqualityComparer界面?

发布于 2024-12-09 08:34:13 字数 199 浏览 1 评论 0原文

'Equal' 和 'GetHashcode' 方法存在于对象类中,并且我们的类型继承了对象基类。 直接实现对象的两个方法和使用IComparer接口有什么区别?

如果我们覆盖对象的 Equal 和 GetHashCode ,并推送到哈希表,它将使用覆盖的 equal 方法?

使用 IEqualityComparer 构造函数新建哈希表有什么不同?

the 'Equal' and 'GetHashcode' method are exist in the object class, and our type inherit the object base class.
what's the different between implement the two methods of the object directly and using the IComparer interface?

if we overriding object's Equal and GetHashCode , and push to a hashtable , it will use the overring 's equal method?

what' the differents of new a hashtable with the IEqualityComparer constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-12-16 08:34:13

当您需要能够对对象进行“排序”时,可以使用 IComparable 接口,它为您提供了一个方法 (CompareTo) 来告诉您两个对象是否 <, =或> 。使用 IEqualityComparer 的构造函数允许您给出特定的 Equals/GetHashCode,该值可以与对象定义的不同。通常,Hashtable 将使用覆盖 EqualsGetHashCode 的对象(或基本 object Equals和GetHashCode)。

举个例子,标准字符串以区分大小写的方式进行比较("A" != "a"),但您可以创建一个 IEqualityComparer 帮助器类能够以不区分大小写的方式对字符串进行哈希处理。 (从技术上讲,此类已经存在于多个变体中:它们被称为 StringComparer.InvariantCultureIgnoreCase 以及返回 StringComparerStringComparer 的所有其他静态方法实现 IComparerIEqualityComparerIComparer 的对象, IEqualityComparer

请注意,Hashtable 使用 IEqualityComparer 可选参数,而不是通用版本 IEqualityComparer< /code>,因为 Hashtable 是预泛型。

The IComparable interface is used when you need to be able to "sort" objects, and it gives you a method (CompareTo) that tells you if two objects are <, = or > . The constructor that uses IEqualityComparer let you give a specific Equals/GetHashCode that can be different than the ones defined by your object. Normally the Hashtable will use your object overridden Equals and GetHashCode (or the base object Equals and GetHashCode).

To make an example, the standard string compares in case sensitive way ("A" != "a"), but you could make an IEqualityComparer helper class to be able to hash your strings in a case insensitive way. (technically this class is already present in multiple variants: they are called StringComparer.InvariantCultureIgnoreCase and all the other static methods of StringComparer that return a StringComparer object that implements the IComparer, IEqualityComparer, IComparer<string>, IEqualityComparer<string>)

As a note, the Hashtable uses a IEqualityComparer optional parameter, not the generic version IEqualityComparer<T>, because Hashtable is pre-generics.

£冰雨忧蓝° 2024-12-16 08:34:13

IComparer 接口(通用接口和非通用接口)允许您比较两个实例。

Compare 方法允许您将对象本身与另一个实例进行比较。当然,当当前实例为 null 时,您将得到一个 NullReferenceException 在这种情况下,因为您在“null”实例上调用 Compare 。实现 IComparer 的类可以克服这个问题。

因此,当您实现 IComparer 接口时,您将拥有一个具有“Compare”方法的类,可以像这样调用该方法:

public class MyObjectComparer : IComparer<MyObject>
{
    public int Compare( MyObject first, MyObject second )
    {
       // implement logic here to determine whether first is less, greater or equal then second.
    }
}

这允许您执行以下操作:

var c = new MyObjectComparer();
var one = new MyObject();
var two = new MyObject();
c.Compare (one, two);

当您实例化 Hashtable 时在您指定 IEqualityComparer 实例的构造函数中,这意味着给定的 IEqualityComparer 将用于确定哈希表中是否已存在某个键。
否则,将使用键对象的 Compare 方法。

the IComparer interfaces (both the generic and the non-generic one) allow you to compare two instances with each other.

The Compare method allows you to compare an object itself with another instance. Offcourse, when the current instance is null, you'll get a NullReferenceException in this case, since you call Compare on a 'null' instance. A class that implements IComparer can overcome this problem.

So, when you implement the IComparer interface, you'll have a class which has a 'Compare' method, which can be called like this:

public class MyObjectComparer : IComparer<MyObject>
{
    public int Compare( MyObject first, MyObject second )
    {
       // implement logic here to determine whether first is less, greater or equal then second.
    }
}

This allows you to do this:

var c = new MyObjectComparer();
var one = new MyObject();
var two = new MyObject();
c.Compare (one, two);

When you instantiate a Hashtable with the constructor where you specify the IEqualityComparer instance, this means that the given IEqualityComparer will be used to determine whether a certain key is already present in the Hashtable.
Otherwise, the Compare method of the key-object will be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文