IEqualityComparer与EqualityComparer? [英] IEqualityComparer vs EqualityComparer?

查看:73
本文介绍了IEqualityComparer与EqualityComparer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了此帖子,但它没有回答我的问题.

I've read this post but it doesn't answer my question.

MSDN 说法:

我们建议您从EqualityComparer(Of T)类派生而不是实现IEqualityComparer(Of T)接口,因为EqualityComparer(Of T)类使用IEquatable(Of T).Equals方法而不是IE来测试是否相等.Object.Equals方法.

We recommend that you derive from the EqualityComparer(Of T) class instead of implementing the IEqualityComparer(Of T) interface, because the EqualityComparer(Of T) class tests for equality using the IEquatable(Of T).Equals method instead of the Object.Equals method.

但是,如果我看一下实现,它们都使用通用类型:

but if I look at the implementation, they both use the generic Type:

 public class AAA:IEqualityComparer<Box>
    {
        public bool Equals(Box x, Box y)
        {
        }

        public int GetHashCode(Box obj)
        {
        }
    }

    public class BBB : EqualityComparer<Box>
    {
        public override bool Equals(Box x, Box y)
        {
        }

        public override int GetHashCode(Box obj)
        {
        }
    }

我想念什么?

推荐答案

我认为其他帖子您提到的是说 EqualityComparer< Box> 实现了 IEqualityComparer< Box> IEqualityComparer ,因此您不必同时实现通用和非通用接口,如果您从 EqualityComparer< Box> 派生.

I think the other post you mention is saying that EqualityComparer<Box> implements IEqualityComparer<Box> and IEqualityComparer, so you don't have to implement both the generic and non-generic interfaces if you derive from EqualityComparer<Box>.

这篇关于IEqualityComparer与EqualityComparer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆