c# - 如何为结构实现 base.GetHashCode()?

标签 c# struct hashcode

<分区>

我最近在 struct 中看到这段代码,我想知道 base.GetHashCode 到底做了什么。

    public override int GetHashCode()
    {
        var hashCode = -592410294;
        hashCode = hashCode * -1521134295 + base.GetHashCode();
        hashCode = hashCode * -1521134295 + m_Value.GetHashCode();
        return hashCode;
    }

最佳答案

coreclr 仓库有 this comment :

Action: Our algorithm for returning the hashcode is a little bit complex. We look for the first non-static field and get it's hashcode. If the type has no non-static fields, we return the hashcode of the type. We can't take the hashcode of a static member because if that member is of the same type as the original type, we'll end up in an infinite loop.

但是,代码不在那里,看起来这不是完全发生的事情。示例:

using System;

struct Foo
{
    public string x;
    public string y;
}

class Test
{
    static void Main()
    {
        Foo foo = new Foo();
        foo.x = "x";
        foo.y = "y";
        Console.WriteLine(foo.GetHashCode());
        Console.WriteLine("x".GetHashCode());
        Console.WriteLine("y".GetHashCode());
    }
}

我的盒子上的输出:

42119818
372029398
372029397

更改 y 的值似乎没有更改 foo 的哈希码.

但是,如果我们将字段设为 int values 而不是,那么第一个字段会影响输出。

简而言之:这很复杂,您可能不应该依赖它在运行时的多个版本中保持不变。除非您真的非常有信心不需要将自定义结构用作基于散列的字典/集合中的键,否则我强烈建议覆盖 GetHashCodeEquals (并实现 IEquatable<T> 以避免装箱)。

关于c# - 如何为结构实现 base.GetHashCode()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47009543/

相关文章:

c# - 类成员声明不是声明吗?

c# - fork TPL 数据流管道时如何正确等待完成?

c# - 将元数据注入(inject).mp4文件

java - 为什么 hashcode() 返回相同的整数?

javascript - 在 python 和 node.js 中复制 java.lang.String.hashCode() 输出的函数

java - 哈希码与对象的引用或地址之间的区别?

c# - 在新选项卡中打开图片,就像右键单击一样 - 在 firefox 中查看图片 (WatiN)

结构需要一生,因为?

c - 函数中的结构和文件中的输出

struct - 为什么我不能多次调用变异函数?