本文介绍了IEqualityComparer和IEquatable在Enumerable.SequenceEqual方法中的作用是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此页面上的 MSDN 它们描述了Enumerable类的SequenceEqual方法.

On this page on MSDN they describe the SequenceEqual method of the Enumerable class.

将其显示在页面的一半以下:

Halfway down the page it states:

然后,他们展示了一个示例,其中他们根本没有实现IEqualityComparer<T>接口,而是实现了IEquatable<T>.我自己完成了测试,没有实现IEqualityComparer或IEquatable,而只是覆盖了Object的Equals,我发现它可以解决问题.这是示例:

Then they show an example where they do not implement the IEqualityComparer<T> interface at all but instead implement IEquatable<T>. I've done the test myself without implementing either IEqualityComparer or IEquatable and simply overriding Object's Equals and I find it does the trick. Here is the sample:

class AlwaysEquals
{
    override public bool Equals(Object o)
    {
        return true;
    }
    public override int GetHashCode()
    {
        return 1;
    }
}

请注意,我的类AlwaysEquals没有实现任何东西,没有IEquatable,没有IEqualityComparer,没有任何东西.但是,当我运行以下代码时:

Note here that my class AlwaysEquals implements nothing, no IEquatable, no IEqualityComparer, nothing. However when I run this code:

AlwaysEquals ae1 = new AlwaysEquals();
AlwaysEquals ae2 = new AlwaysEquals();
AlwaysEquals ae3 = new AlwaysEquals();
AlwaysEquals[] Ae1 = new AlwaysEquals[] {ae3, ae2, ae3};
AlwaysEquals[] Ae2 = new AlwaysEquals[] {ae1, ae1, ae1};
Console.WriteLine(Ae1.SequenceEqual(Ae2));

..我得到的是True而不是False,正如我从阅读文档中所期望的那样.这实际上如何工作?

.. I get True and not False as I would expect from reading the documentation. How does this actually work?

推荐答案

IEquatable 被Dictionary之类的通用集合用来确定两个对象是否相等.如果对象未实现IEquatable,则使用Object.Equals方法.

IEquatable is used by the generic collections like Dictionary to determine if two objects are equal. If the object doesn't implement IEquatable, Object.Equals method is used.

为什么要实现IEquatable?它的性能比Object.Equals好,因为不需要强制转换对象.

Why should you implement IEquatable? It has better performance than Object.Equals because the object doesn't need to be casted.

何时不实施IEquatable? 某些开发人员认为,您应该仅在密封类上实现它..

如果在SequenceEquals中指定了 IEqualityComparer ,则它是用于检查两个对象(而不是Object.Equal)是否相等的对象,它是IEquatable的实现.在SequenceEqual中使用它的示例在此处 http://msdn.microsoft.com/zh-cn/library/bb342073%28v=vs.110%29.aspx .请注意,方法签名接受IEqualityComparer.

If IEqualityComparer is specified in SequenceEquals, it is the one used to check the equality of two objects instead of Object.Equal and it's IEquatable implementation. The example for using it in SequenceEqual is in here http://msdn.microsoft.com/en-us/library/bb342073%28v=vs.110%29.aspx. Note that the method signature accepts an IEqualityComparer.

许多馆藏,例如词典在其构造函数中也接受IEqualityComparer

Many collections like Dictionary also accepts IEqualityComparer in it's constructor

回答您的问题

如果未向SequenceEquals提供IEqualityComparer,它将使用EqualityComparer.Default.

If you didn't provide an IEqualityComparer to SequenceEquals, it will use EqualityComparer.Default.

反编译代码:

public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
{
  return Enumerable.SequenceEqual<TSource>(first, second, (IEqualityComparer<TSource>) null);
}

public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
{
  if (comparer == null)
    comparer = (IEqualityComparer<TSource>) EqualityComparer<TSource>.Default;
...

EqualityComparer.Default检查类型T是否实现 System.IEquatable 接口,如果是,则返回使用该实现的EqualityComparer.否则,它返回一个EqualityComparer,它使用T提供的 Object.Equals Object.GetHashCode 的替代.这就是调用Object.Equals的原因.

EqualityComparer.Default checks whether type T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses that implementation. Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T. This is why your Object.Equals is called.

这篇关于IEqualityComparer和IEquatable在Enumerable.SequenceEqual方法中的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:03