比较两个序列是否相等 [英] Compare if two sequences are equal

查看:89
本文介绍了比较两个序列是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在由于标题将其标记为重复之前,请考虑以下简短程序:

Before marking this as duplicate because of its title please consider the following short program:

static void Main()
{
    var expected = new List<long[]> { new[] { Convert.ToInt64(1), Convert.ToInt64(999999) } };
    var actual = DoSomething();
    if (!actual.SequenceEqual(expected)) throw new Exception();
}

static IEnumerable<long[]> DoSomething()
{
    yield return new[] { Convert.ToInt64(1), Convert.ToInt64(999999) };
}

我有一个方法可以返回一个long类型的数组序列.为了测试它,我在Main中编写了一些类似于该代码的测试代码.

I have a method which returns a sequence of arrays of type long. To test it I wrote some test-code similar to that one within Main.

但是我得到了例外,但我不知道为什么.预期的顺序不应该与实际返回的顺序相提并论吗?或者我错过了任何事情吗?

However I get the exception, but I don´t know why. Shouldn´t the expected sequence be comparable to the actually returned one or did I miss anything?

在我看来,方法和epxected都只包含一个包含long类型数组的单个元素,不是吗?

To me it looks as both the method and the epxected contain exactly one single element containing an array of type long, doesn´t it?

那么我该如何实现不获取异常含义,即比较枚举中的元素以返回相等性?

So how do I achieve to not get the exception meaning to compare the elements within the enumeration to return equality?

推荐答案

实际的问题是,您正在比较两个long[],而Enumerable.SequenceEquals将使用ObjectEqualityComparer<Int64[]>(您可以通过检查EqualityComparer<long[]>.Default(这是Enumerable.SequenceEquals内部使用的),它将比较这两个数组的引用,而不是存储在数组内部的实际 values ,这显然是不相同的.

The actual problem is the fact that you're comparing two long[], and Enumerable.SequenceEquals will use an ObjectEqualityComparer<Int64[]> (you can see that by examining EqualityComparer<long[]>.Default which is what is being internally used by Enumerable.SequenceEquals), which will compare references of those two arrays, and not the actual values stored inside the array, which obviously aren't the same.

要解决此问题,您可以编写自定义EqualityComparer<long[]>:

To get around this, you could write a custom EqualityComparer<long[]>:

static void Main()
{
    var expected = new List<long[]> 
                       { new[] { Convert.ToInt64(1), Convert.ToInt64(999999) } };
    var actual = DoSomething();

    if (!actual.SequenceEqual(expected, new LongArrayComparer()))
        throw new Exception();
}

public class LongArrayComparer : EqualityComparer<long[]>
{
    public override bool Equals(long[] first, long[] second)
    {
        return first.SequenceEqual(second);
    }

    // GetHashCode implementation in the courtesy of @JonSkeet
    // from http://stackoverflow.com/questions/7244699/gethashcode-on-byte-array
    public override int GetHashCode(long[] arr)
    {
        unchecked
        {
            if (array == null)
            {
                return 0;
            }

            int hash = 17;
            foreach (long element in arr)
            {
                hash = hash * 31 + element.GetHashCode();
            }

            return hash;
        }
    }
}

这篇关于比较两个序列是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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