byte []数组上的GetHashCode()

时间:2011-08-30 14:20:01

标签: c# hash

GetHashCode()数组上调用byte[]时计算的内容是什么? 具有相同内容的2个数据阵列不提供相同的散列。

5 个答案:

答案 0 :(得分:57)

.NET中的数组不会覆盖EqualsGetHashCode,因此您获得的值基本上基于引用相等(即Object中的默认实现) - 对于您需要滚动自己的代码(或从第三方找到一些代码)的价值平等。如果您尝试将字节数组用作字典中的键等,则可能需要实现IEqualityComparer<byte[]>

编辑:这是一个可重用的数组相等比较器,只要数组元素适当地处理相等就应该没问题。请注意,在将数组用作字典中的键后,不得更改数组,否则您将无法再次找到它 - 即使使用相同的引用也是如此。

using System;
using System.Collections.Generic;

public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>
{
    // You could make this a per-instance field with a constructor parameter
    private static readonly EqualityComparer<T> elementComparer
        = EqualityComparer<T>.Default;

    public bool Equals(T[] first, T[] second)
    {
        if (first == second)
        {
            return true;
        }
        if (first == null || second == null)
        {
            return false;
        }
        if (first.Length != second.Length)
        {
            return false;
        }
        for (int i = 0; i < first.Length; i++)
        {
            if (!elementComparer.Equals(first[i], second[i]))
            {
                return false;
            }
        }
        return true;
    }

    public int GetHashCode(T[] array)
    {
        unchecked
        {
            if (array == null)
            {
                return 0;
            }
            int hash = 17;
            foreach (T element in array)
            {
                hash = hash * 31 + elementComparer.GetHashCode(element);
            }
            return hash;
        }
    }
}

class Test
{
    static void Main()
    {
        byte[] x = { 1, 2, 3 };
        byte[] y = { 1, 2, 3 };
        byte[] z = { 4, 5, 6 };

        var comparer = new ArrayEqualityComparer<byte>();

        Console.WriteLine(comparer.GetHashCode(x));
        Console.WriteLine(comparer.GetHashCode(y));
        Console.WriteLine(comparer.GetHashCode(z));
        Console.WriteLine(comparer.Equals(x, y));
        Console.WriteLine(comparer.Equals(x, z));
    }
}

答案 1 :(得分:17)

与其他非原始内置类型一样,它只返回任意内容。它肯定不会尝试散列数组的内容。见this answer.

答案 2 :(得分:10)

byte[]GetHashCode()继承object,但不会覆盖它。所以你得到的基本上是object的实现。

答案 3 :(得分:1)

如果它不是同一个实例,它将返回不同的哈希值。我猜它是基于存储地址以某种方式存储它。

答案 4 :(得分:1)

简单的解决方案

[[2.07449943e-01 1.45395458e-01 1.04659587e-01 8.97483081e-02
8.33745003e-02 5.82174100e-02 5.39956503e-02 4.87998128e-02
4.64136861e-02 4.55792621e-02]
[2.96225220e-01 2.38899514e-01 1.29523054e-01 1.05340779e-01
9.04821381e-02 6.32023811e-02 3.89073454e-02 3.25137116e-02
4.40399708e-05 4.39952055e-05]
[2.01232404e-01 1.35382757e-01 1.30107656e-01 1.17249586e-01
9.61307958e-02 5.42762764e-02 5.38591929e-02 5.00357226e-02
4.47071381e-02 4.40189019e-02]
[2.15390638e-01 1.36802554e-01 1.04552917e-01 9.10867527e-02
8.69785920e-02 8.43476653e-02 8.27447996e-02 6.82006180e-02
4.97598834e-02 3.98983061e-02]
[1.83206141e-01 1.69056609e-01 8.36808681e-02 6.95630759e-02
6.75979853e-02 6.68922439e-02 6.13995455e-02 5.46063147e-02
4.98242378e-02 4.57465537e-02]
[3.58453363e-01 1.51751891e-01 9.13974121e-02 7.82546997e-02
7.66202509e-02 5.77278510e-02 3.57825160e-02 3.28263938e-02
3.23474333e-02 2.97523513e-02]
[1.93134964e-01 1.09733373e-01 9.76252779e-02 8.41044113e-02
7.75038153e-02 6.58460185e-02 5.49041331e-02 5.19012176e-02
4.82283793e-02 4.29505669e-02]
[4.45734113e-01 1.43566564e-01 7.97773823e-02 7.55326077e-02
6.59788921e-02 5.13889231e-02 5.05173914e-02 4.82447147e-02
3.57971117e-02 3.13587079e-05]
[1.97211459e-01 1.83093011e-01 1.00199319e-01 8.06274563e-02
6.74550235e-02 6.67441264e-02 6.04876168e-02 5.56992702e-02
4.95750830e-02 4.94292155e-02]
[1.63107052e-01 9.19746161e-02 8.31030086e-02 8.16068277e-02
5.92814237e-02 4.98266183e-02 4.92400900e-02 4.63733897e-02
4.60482053e-02 4.56368625e-02]]