简体   繁体   English

可以为同一double的GetHashCode()生成不同的整数吗?

[英]Can GetHashCode() for the same double result in a different integer?

Let's say we have a double value in C#. 假设我们在C#中有一个double值。

Is it possible that the GetHashCode() may return different integer values for this double on different computers/windows/architectures? GetHashCode()是否有可能在不同的计算机/窗口/体系结构上为此double返回不同的整数值?

    public unsafe override int GetHashCode() {
        double d = m_value; 
        if (d == 0) { 
            // Ensure that 0 and -0 have the same hash code
            return 0; 
        }
        long value = *(long*)(&d);
        return unchecked((int)value) ^ ((int)(value >> 32));
    }

Object.GetHashCode Method says: Object.GetHashCode方法说:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. GetHashCode方法的默认实现不能保证不同对象的唯一返回值。 Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. 此外,.NET Framework不保证GetHashCode方法的默认实现,并且它返回的值在.NET Framework的不同版本之间将相同。 Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes. 因此,此方法的默认实现不得用作哈希目的的唯一对象标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM